Ok i have a question how come when i press the button it adds one but
then it doesnt do it again? Does it have to be in some kind of loop?
require ‘tk’
number = 1
hello = TkRoot.new do
title “Hello World”
minsize(400,400)
end
lbl = TkLabel.new() { justify ‘center’
text “#{number}”;
pack(‘padx’=>5, ‘pady’=>5, ‘side’ => ‘top’) }
TkButton.new() {
text “Add 1”
command proc { lbl.configure(‘text’=>"#{number + 1}") }
pack(‘side’=>‘right’, ‘padx’=>10, ‘pady’=>10)
}
Tk.mainloop
Thanks
Isaac T. wrote:
text “#{number}”;
pack(‘padx’=>5, ‘pady’=>5, ‘side’ => ‘top’) }
TkButton.new() {
text “Add 1”
command proc { lbl.configure(‘text’=>"#{number + 1}") }
pack(‘side’=>‘right’, ‘padx’=>10, ‘pady’=>10)
}
Tk.mainloop
Thanks
It is doing it. It changes the text to number + 1 (that is, 1 + 1) each
time. If you actually want to change the variable, try
command proc { lbl.configure(‘text’=>"#{number += 1}") }
-Justin
lol i tried that but i did =+ 1 so it was a typo and didnt work lol
well any way i really want it to be a input box and not a label but text
doesnt work with the input boxes…i think.
require ‘tk’
number = 1
hello = TkRoot.new do
title “Hello World”
the min size of window
minsize(400,400)
end
lbl = TkEntry.new() { justify ‘center’
text “#{number}”;
pack(‘padx’=>5, ‘pady’=>5, ‘side’ => ‘top’) }
TkButton.new() {
text “Add One”
command proc { lbl.configure(‘text’=>“#{number += 1}”) }
pack(‘side’=>‘right’, ‘padx’=>10, ‘pady’=>10)
}
Tk.mainloop
so how would you do this? Ive tried to google a good guide but they are
only like a couple examples and maybe a page. Other then help with this
problem (which im going to continue to work on while this is posted) i
want to know if anyone has a good guide that has examples but also
explains them. Or maybe a website like
Programming Ruby: The Pragmatic Programmer's Guide for tk so i can see all of
the commands like label and button.
Thanks
Morton G. wrote:
Regards, Morton
Thanks! this is really helpful it allows me to see how to do boarders
and this is probably more of what i would use instead of input boxes but
even though this is something that is very helpful i would also like to
see how to do this with inputboxes. Like is there a way to have a number
preset in the box and then when you press the button it adds one to it
much like this just with an inputbox instead of a boarder.
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
On Feb 22, 2008, at 1:29 PM, Isaac T. wrote:
so how would you do this?
require 'tk'
class Example
def initialize
@number = 1
root = Tk.root
root.title(‘Ruby/Tk Example’)
@lbl = TkLabel.new(root) {
borderwidth 1
relief :solid
width 10
pack(:pady => 10)
}
@lbl.text = @number.to_s
@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 += 1
@lbl.text = @number.to_s
end
end
Example.new
Ive tried to google a good guide but they are
only like a couple examples and maybe a page. Other then help with
this
problem (which im going to continue to work on while this is posted) i
want to know if anyone has a good guide that has examples but also
explains them. Or maybe a website like
Programming Ruby: The Pragmatic Programmer's Guide for tk so i can see
all of
the commands like label and button.
There are a lot of examples at
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/
tk/sample/
Regards, Morton
Thank you this is exactly what i was wanting! Im going to try to find a
good class tutorial so i can understand them a little more just the only
thing i didnt understand to well was this
Morton G. wrote:
# Make Cmnd+Q work as expected on OS X.
if RUBY_PLATFORM =~ /darwin/
root.bind('Command-q') { root.destroy }
i under stand what it does and i under stand the root.bind(…)…
but i just dont under stand the “=~ /darwin/” What is it telling the
computer to look for?.
Also if you can do this does this mean that i could make a program that
lets say if you press F10 then it can turn a continues loop on and off?
Like if pressed it sends z over and over again and if you press it again
it stops.
On Feb 26, 2008, at 12:54 PM, Isaac T. wrote:
i under stand what it does and i under stand the root.bind(…)…
but i just dont under stand the “=~ /darwin/” What is it telling the
computer to look for?.
‘=~’ is the regular expression match operator. It’s a way of asking:
does the pre-defined Ruby constant RUBY_PLATFORM contain the string
‘darwin’. When Ruby runs under OS X, the answer will be yes – that
is, ‘RUBY_PLATFORM =~ /darwin/’ evaluates to non-nil on a Macintosh
running OS X, but not on one running Linux or Windows (and, yes, a
Macintosh can run Linux or Windows).
Also if you can do this does this mean that i could make a program
that
lets say if you press F10 then it can turn a continues loop on and
off?
Like if pressed it sends z over and over again and if you press it
again
it stops.
Yes, you could do that. You can use the bind method to bind an action
to any keyboard event. Further bind can be used to bind actions to
other operating system events as well.
Regards, Morton
On Feb 22, 2008, at 10:07 PM, Isaac T. wrote:
number = 1
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.
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
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
On Mar 1, 2008, at 4:26 PM, Isaac T. wrote:
But i just have one question.
What is the reason to use classes? like what does a class change
does it
just make it neater? or does it define variables in a different way?
Just one question. But it really asks: what is Ruby about? It would
seem you need to learn the basics; in particular, the basics of OOP
(Object Oriented Programming). Perhaps some other participants in
this mailing list will feel up to posting an explanation of what OOP
is, but it’s too big a topic for me. All I can do is recommend you
obtain copies of these two books:
David Flanagan, Yukihiro M.: The Ruby P.ming Language
(ISBN-10: 0596516177 ISBN-13: 978-0596516178)
Dave T. et al., Programming Ruby: The Pragmatic Programmers’
Guide (ISBN-10: 0974514055
ISBN-13: 978-0974514055)
Regards, Morton
Thanks your help i can start make basic programs with ruby (gui)
But i just have one question.
What is the reason to use classes? like what does a class change does it
just make it neater? or does it define variables in a different way?