This is something else i tried but of course it didnt work
require 'tk'
hello = TkRoot.new do
title "Hello World"
# the min size of window
minsize(400,400)
end
number = 1
def addnumber
@text.value = number
end
@text = TkVariable.new
lbl = TkEntry.new('textvariable' => @text) { justify 'center'
pack('padx'=>5, 'pady'=>5, 'side' => 'top') }
TkButton.new() {
text "Add One"
command proc {addnumber}
pack('side'=>'right', 'padx'=>10, 'pady'=>10)
}
Tk.mainloop
It didn't work mainly because you are confused about how Ruby scopes
variables. My example defines a new class for good reason -- to make
sure the variables I use are visible where and when I need them.
<code>
require 'tk'
class Example
def initialize
@number = TkVariable.new(1)
root = Tk.root
root.title('Ruby/Tk Example')
# @entry = TkLabel.new(root) {
# width 10
# borderwidth 1
# relief :solid
# pack
pady => 10)
# }
@entry = TkEntry.new(root) {
width 10
justify :center
pack
pady => 10)
}
@entry.textvariable = @number
@btn = TkButton.new(root) {
text "Add One"
pack
pady => 10)
}
@btn.command = lambda { action }
# Set initial window geometry; i.e., size and placement.
win_w, win_h, win_y = 200, 85, 50
win_x = (root.winfo_screenwidth - win_w) / 2
root.geometry("#{win_w}x#{win_h}+#{win_x}+#{win_y}")
# Set resize permissions.
root.resizable(false, false)
# Make Cmnd+Q work as expected on OS X.
if RUBY_PLATFORM =~ /darwin/
root.bind('Command-q') { root.destroy }
end
Tk.mainloop
end
def action
@number.numeric += 1
end
end
Example.new
</code>
The commented-out code will also work if you uncomment it and comment
out the @entry = TkEntry { ... } code instead. This is because
TkEntry is a direct subclass of TkLabel and inherits its textvariable
property from TkLabel. The difference is that you can edit the
numbers when a TkEntry object is used. For instance, if you edit a
displayed number to 42, when you click on the button, the next number
will be 43.
Regards, Morton