PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Ruby/Tk
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Ruby/Tk

Réponse
 
LinkBack Outils de la discussion
Vieux 22/02/2008, 16h47   #1
Isaac Toothyxdip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Ruby/Tk

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
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/02/2008, 16h53   #2
Phlip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby/Tk

Isaac Toothyxdip wrote:

> 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 !

--
Phlip
http://assert2.rubyforge.org/
  Réponse avec citation
Vieux 22/02/2008, 17h13   #3
Justin Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby/Tk

Isaac Toothyxdip wrote:
> 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



  Réponse avec citation
Vieux 22/02/2008, 18h29   #4
Isaac Toothyxdip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby/Tk

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 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
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 23/02/2008, 00h57   #5
Morton Goldberg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby/Tk

On Feb 22, 2008, at 1:29 PM, Isaac Toothyxdip wrote:

> 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
</code>

> Ive tried to google a good guide but they are
> only like a couple examples and maybe a page. Other then 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/vie.../ruby_1_8/ext/
tk/sample/

Regards, Morton

  Réponse avec citation
Vieux 23/02/2008, 03h07   #6
Isaac Toothyxdip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby/Tk

Morton Goldberg wrote:
> Regards, Morton


Thanks! this is really ful 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 ful 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

--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 23/02/2008, 05h19   #7
Morton Goldberg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby/Tk

On Feb 22, 2008, at 10:07 PM, Isaac Toothyxdip wrote:

> 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

  Réponse avec citation
Vieux 26/02/2008, 17h54   #8
Isaac Toothyxdip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby/Tk

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 Goldberg 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.

--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 26/02/2008, 21h19   #9
Morton Goldberg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby/Tk

On Feb 26, 2008, at 12:54 PM, Isaac Toothyxdip wrote:

> 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 Goldberg 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?.


'=~' 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

  Réponse avec citation
Vieux 01/03/2008, 21h26   #10
Isaac Toothyxdip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby/Tk

Thanks your 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?
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 02/03/2008, 01h31   #11
Morton Goldberg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby/Tk

On Mar 1, 2008, at 4:26 PM, Isaac Toothyxdip 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 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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h19.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,23433 seconds with 19 queries