Tk dialogs

  • Thread starter R. Mark Volkmann
  • Start date
R

R. Mark Volkmann

How can I add widgets like TkEntry to a TkDialog? I've written a class that
inherits from TkDialog, but I can't figure out how to add widgets in the
initialize method. Maybe that's not possible and I should use a TkFrame
instead to implement a custom dialog box.
 
H

Hidetoshi NAGAI

Hi,

From: "R. Mark Volkmann" <[email protected]>
Subject: Tk dialogs
Date: Thu, 9 Dec 2004 23:03:30 +0900
Message-ID: said:
How can I add widgets like TkEntry to a TkDialog? I've written a class that
inherits from TkDialog, but I can't figure out how to add widgets in the
initialize method.

It is possible, but a little difficult.
I recommend you to create your dialogbox class.
Maybe that's not possible and I should use a TkFrame
instead to implement a custom dialog box.

Here you are. ;-) Please improve it for your purpose.

# The sample requires tkalignbox.rb which is included at 'ext/tk/samples'
# directory in Ruby source archive.

-------------------------------------------------------------------------
require 'tk'
require 'tkalignbox'

class MyDialogBox < TkToplevel
def _close(str)
@status.value = str
@top.withdraw
end

def initialize(parent, *btn_names) # parent, btn_name, ..., keys
args = []

if parent != nil && !parent.kind_of?(TkWindow)
btn_names.unshift(parent)
parent = nil
end
args << parent unless parent
args << btn_names.pop if btn_names[-1].kind_of?(Hash)

super(*args)

@status = TkVariable.new

@frame = TkFrame.new(@top).pack:)side=>:top, :fill=>:both, :expand=>true)

@btn_frame = TkFrame.new(@top, :borderwidth=>2, :relief=>:ridge)
@btn_frame.pack:)side=>:bottom, :fill=>:x, :expand=>true)

@btn_box = TkHBox.new(@btn_frame, :borderwidth=>0,
:padx=>7, :pady=>3).pack

@buttons = btn_names.collect{|name|
TkButton.new(@top, :text=>name, :command=>proc{_close(name)})
}

@btn_box.add(*@buttons)
end

def create_self(keys={})
@top = TkToplevel.new({:widgetname=>@path}.update(keys))
@top.protocol:)WM_DELETE_WINDOW, proc{}) # ignore
@top.bind('Destroy'){@status.value = ''} # to exit @status.wait
@top.withdraw
end

def frame
@frame
end

def show(geom=nil)
@top.geometry(geom) if geom
@top.deiconify
@top.resizable(false, false)
@top.raise
@top.grab
@status.wait
@top.grab_release
@top.withdraw

@status.value
end
end

################################################
# test
################################################
if __FILE__ == $0
dialog = MyDialogBox.new(*%w(ok retry cancel foo bar))
f = dialog.frame
l = TkLabel.new(f, :text=>'This is a sample of MyDialogBox.')
l.pack:)padx=>100, :pady=>20, :fill=>:x)
ff = TkFrame.new(f).pack:)padx=>10, :pady=>5, :fill=>:x, :expand=>true)
TkLabel.new(ff, :text=>'Input:').pack:)side=>:left)
e = TkEntry.new(ff).pack:)side=>:left, :fill=>:both, :expand=>true)

TkButton.new:)text=>'show dialog',
:command=>proc{
e.focus
btn = dialog.show
p [btn, e.value]
}).pack

Tk.mainloop
end
 
R

R. Mark Volkmann

How can I exit a Ruby application that uses Tk without getting the following
message?

"This application has requested the Runtime to teminate it in an unusual
way."

Using Kernel#exit gives me this message.
 
B

Bill Atkins

Tk.exit ?

How can I exit a Ruby application that uses Tk without getting the following
message?

"This application has requested the Runtime to teminate it in an unusual
way."

Using Kernel#exit gives me this message.
 
R

R. Mark Volkmann

Quoting Bill Atkins said:
Are you using any other libraries aside from Tk?

No, just Tk.

The following simple snippet of code demonstrates the problem. I see the
problem under Windows XP, but not under Windows 2000. On both platforms I'm
using ruby 1.8.2 (2004-11-06) from the Windows one-click installer.

require 'tk'
root = TkRoot.new
button = TkButton.new(root)
button.command lambda {Tk.exit}
button.text 'Exit'
button.pack
Tk.mainloop
 
H

Hidetoshi NAGAI

Hi,

From: "R. Mark Volkmann" <[email protected]>
Subject: Re: clean Tk exit
Date: Tue, 14 Dec 2004 01:41:20 +0900
Message-ID: said:
The following simple snippet of code demonstrates the problem. I see the
problem under Windows XP, but not under Windows 2000. On both platforms I'm
using ruby 1.8.2 (2004-11-06) from the Windows one-click installer.

I don't know that your tcltklib binary on your ruby is a patched
version or not.
Does anyone know that the current one-click installer (182-RC10)
is applied the patch of [ruby-talk: 119637] ?
If use the patched version, that is a new problem.
I want to know the same problem occurs or not on Tk8.4 or 8.5
(see [ruby-talk: 120774]).
 
C

Curt Hibbs

Hidetoshi said:
Hi,

From: "R. Mark Volkmann" <[email protected]>
Subject: Re: clean Tk exit
Date: Tue, 14 Dec 2004 01:41:20 +0900
Message-ID: said:
The following simple snippet of code demonstrates the problem. I see the
problem under Windows XP, but not under Windows 2000. On both platforms I'm
using ruby 1.8.2 (2004-11-06) from the Windows one-click installer.

I don't know that your tcltklib binary on your ruby is a patched
version or not.
Does anyone know that the current one-click installer (182-RC10)
is applied the patch of [ruby-talk: 119637] ?
If use the patched version, that is a new problem.
I want to know the same problem occurs or not on Tk8.4 or 8.5
(see [ruby-talk: 120774]).

No patches have been applied in the one-click installer.

Curt
 
M

Mike Jones

R. Mark Volkmann said:
No, just Tk.

The following simple snippet of code demonstrates the problem. I see the
problem under Windows XP, but not under Windows 2000. On both platforms I'm
using ruby 1.8.2 (2004-11-06) from the Windows one-click installer.

require 'tk'
root = TkRoot.new
button = TkButton.new(root)
button.command lambda {Tk.exit}
button.text 'Exit'
button.pack
Tk.mainloop

I just tried the following with the latest one click installer for Ruby.
I am running Windows XP SP2 and this code run from irb worked fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top