Tk dialogs

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.

···

--
R. Mark Volkmann
Partner, Object Computing, Inc.

Hi,

···

From: "R. Mark Volkmann" <mark@ociweb.com>
Subject: Tk dialogs
Date: Thu, 9 Dec 2004 23:03:30 +0900
Message-ID: <1102600881.41b85ab18ec2d@mail.ociweb.com>

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. :wink: 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
-------------------------------------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

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.

Tk.exit ?

···

On Sun, 12 Dec 2004 12:38:38 +0900, R. Mark Volkmann <mark@ociweb.com> wrote:

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.

--
$stdout.sync = true
"Just another Ruby hacker.".each_byte do |b|
  ('a'..'z').step do|c|print c+"\b";sleep 0.007 end;print b.chr
end; print "\n"

Tk.exit ?

Nope. I still get the same message when the application exits.
Any other ideas?

···

----- Original Message ----- From: "Bill Atkins" <batkins57@gmail.com>
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Sunday, December 12, 2004 2:52 PM
Subject: Re: clean Tk exit

On Sun, 12 Dec 2004 12:38:38 +0900, R. Mark Volkmann <mark@ociweb.com> > wrote:

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.

Are you using any other libraries aside from Tk?

···

On Mon, 13 Dec 2004 11:23:16 +0900, R. Mark Volkmann <mark@ociweb.com> wrote:

----- Original Message -----
From: "Bill Atkins" <batkins57@gmail.com>
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Sunday, December 12, 2004 2:52 PM
Subject: Re: clean Tk exit

> Tk.exit ?

Nope. I still get the same message when the application exits.
Any other ideas?

> On Sun, 12 Dec 2004 12:38:38 +0900, R. Mark Volkmann <mark@ociweb.com> > > wrote:
>> 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.

--
$stdout.sync = true
"Just another Ruby hacker.".each_byte do |b|
  ('a'..'z').step do|c|print c+"\b";sleep 0.007 end;print b.chr
end; print "\n"

Quoting Bill Atkins <batkins57@gmail.com>:

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

···

On Mon, 13 Dec 2004 11:23:16 +0900, R. Mark Volkmann <mark@ociweb.com> wrote:
> ----- Original Message -----
> From: "Bill Atkins" <batkins57@gmail.com>
> To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
> Sent: Sunday, December 12, 2004 2:52 PM
> Subject: Re: clean Tk exit
>
> > Tk.exit ?
>
> Nope. I still get the same message when the application exits.
> Any other ideas?
>
>
>
> > On Sun, 12 Dec 2004 12:38:38 +0900, R. Mark Volkmann <mark@ociweb.com> > > > wrote:
> >> 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.
>
>

--
$stdout.sync = true
"Just another Ruby hacker.".each_byte do |b|
  ('a'..'z').step do|c|print c+"\b";sleep 0.007 end;print b.chr
end; print "\n"

--
R. Mark Volkmann
Partner, Object Computing, Inc.

Hi,

···

From: "R. Mark Volkmann" <mark@ociweb.com>
Subject: Re: clean Tk exit
Date: Tue, 14 Dec 2004 01:41:20 +0900
Message-ID: <1102955875.41bdc563a99e7@mail.ociweb.com>

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]).
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

R. Mark Volkmann wrote:

Quoting Bill Atkins <batkins57@gmail.com>:

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

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.

Hidetoshi NAGAI wrote:

Hi,

From: "R. Mark Volkmann" <mark@ociweb.com>
Subject: Re: clean Tk exit
Date: Tue, 14 Dec 2004 01:41:20 +0900
Message-ID: <1102955875.41bdc563a99e7@mail.ociweb.com>
> 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