Rubytk dialog

Hello

There is a some unneccessary code in it especcially the pack options are just for experiments

class ValueEntry
  def initialize(window, txt, init)

    @answer = TkVariable.new("")

    rx = TkWinfo.rootx window
    ry = TkWinfo.rooty window

    rh = TkWinfo.height window
    rw = TkWinfo.width window

    top = TkToplevel.new(window) {
      relief 'raised'
      border 1
    }

    top.grab

    label = TkLabel.new(top) {
      text txt
      pack('anchor'=>'c')
    }
    @entry = TkEntry.new(top) {
      pack
    }

    @entry.value = init

    @entry.focus

    bot = TkFrame.new(top) {
      border 1
    }
        TkFrame.new(bot) {
      border 1
      pack('side'=>'left', 'expand'=>'yes', 'padx'=>10, 'pady'=> 10)
    }

    a = TkButton.new(bot) {
      text "Accept"
      pack('side'=>'left', 'expand'=>'yes')
    }

     a.bind("ButtonRelease-1") do
      @answer.value = "Accept"
    end

    d = TkButton.new(bot) {
      text "Discard"
      pack('side'=>'left', 'expand'=>'yes')
    }

    d.bind("ButtonRelease-1") do
      @answer.value = "Discard"
    end

    bot.pack

    Tk.update

    mh = TkWinfo.height top
    mw = TkWinfo.width top

    nx = rx+rw/2-mw/2
    ny = ry+rh/2-mh/2

    geom = mw.to_s + 'x'+ mh.to_s + '+' + nx.to_s + '+' + ny.to_s

    top.geometry(geom)

    @answer.wait

    result = @entry.value

    top.grab(:release)

    top.destroy

    # NOTE: this is totally crap

    if @answer.value == "Accept"
      return result
    else
      return nil
    end
  end
end

Message-ID: <42A5DC87.5070702@freenet.de>

There is a some unneccessary code in it especcially the pack options are
just for experiments

Hmmm.....
If I were you, reuse the created ValueEntry object.
For example,

···

From: Enrico Schwass <deckard73@freenet.de>
Subject: Re: rubytk dialog
Date: Wed, 8 Jun 2005 02:40:55 +0900
-------------------------------------------------------
require 'tk'

class ValueEntry
  def initialize
    @top = TkToplevel.new(:borderwidth=>1, :relief=>:raised){
      withdraw
      bind('Visibility'){self.raise} # to keep on top
    }

    @label = TkLabel.new(@top, :text=>'').pack(:anchor=>:c)

    @value = TkVariable.new
    @entry = TkEntry.new(@top, :textvariable=>@value).pack

    @answer = ans = TkVariable.new
    TkFrame.new(@top, :borderwidth=>1){|bot|

      TkFrame.new(bot, :borderwidth=>1).pack(:side=>:left, :expand=>true,
                                             :padx=>10, :pady=>10)

      TkButton.new(bot, :text=>'Accept'){
        bind('ButtonRelease-1'){ ans.bool = true }
      }.pack(:side=>:left, :expand=>true)

      TkButton.new(bot, :text=>'Discard'){
        bind('ButtonRelease-1'){ ans.bool = false }
      }.pack(:side=>:left, :expand=>true)

    }.pack
  end

  def show(window, txt, init)
    @answer.value = ''
    @label.text = txt
    @value.value = init

    rx = TkWinfo.rootx(window)
    ry = TkWinfo.rooty(window)

    rh = TkWinfo.height(window)
    rw = TkWinfo.width(window)

    mh = TkWinfo.reqheight(@top) # should be reqheight instead of height
    mw = TkWinfo.reqwidth(@top) # should be reqwidth instead of width

    @top.geometry("+#{rx+rw/2-mw/2}+#{ry+rh/2-mh/2}")
    @top.deiconify

    Tk.update_idletasks

    @entry.focus

    @top.grab

    @answer.wait

    @top.grab_release
    @top.withdraw

    self.value
  end

  def value
    (@answer.bool)? @value.value: nil
  end

  def destroy
    @top.destroy
  end
end

if $0 == __FILE__
  t = TkText.new.pack
  ve = ValueEntry.new
  TkButton.new(:command=>proc{ p ve.show(t, 'foo', 'bar') }).pack
  Tk.mainloop
end
-------------------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)