Ruby/Tk

I

Isaac Toothyxdip

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
 
P

Phlip

Isaac said:
number = 1 ....
TkButton.new() {
command proc { lbl.configure('text'=>"#{number + 1}") }
}

I rewrite without Tk:

number = 1
q = proc{ number + 1 }
assert{ q.call == 2 }
assert{ q.call == 2 }

Each q.call finds the outer number, adds one, and returns it. So the second
q.call does not appear to do anything "again", such as go to 3.

Try number += 1 !
 
J

Justin Collins

Isaac said:
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

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
 
I

Isaac Toothyxdip

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
http://www.ruby-doc.org/docs/ProgrammingRuby/ for tk so i can see all of
the commands like label and button.

Thanks
 
M

Morton Goldberg

so how would you do this?

<code>
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
http://www.ruby-doc.org/docs/ProgrammingRuby/ 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
 
I

Isaac Toothyxdip

Morton said:
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
 
M

Morton Goldberg

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
 
I

Isaac Toothyxdip

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 said:
# 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.
 
M

Morton Goldberg

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


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
 
I

Isaac Toothyxdip

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?
 
M

Morton Goldberg

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 Matsumoto: The Ruby Programming Language
(ISBN-10: 0596516177 ISBN-13: 978-0596516178)
Dave Thomas et al., Programming Ruby: The Pragmatic Programmers'
Guide (ISBN-10: 0974514055
ISBN-13: 978-0974514055)

Regards, Morton
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top