From: Jeppe Jakobsen Date: 2006-04-14T22:16:49+09:00 Subject: Re: Q about tk buttons ------=_Part_35784_8290629.1145020606024 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Thank you that really cleared things up :) But I don't understand this line though >entry.bind('Return', proc{button.invoke}) It doesn't seem to make any difference whether I include it in the program or not. I tried to read about it in the documentation, but I only understoo= d that it had something to do with invoking the command of the button. 2006/4/14, Hidetoshi NAGAI : > > Hmmm... There are some problems. > > From: "Jeppe Jakobsen" > Subject: Re: Q about tk buttons > Date: Thu, 13 Apr 2006 22:33:18 +0900 > Message-ID: <99b4ad3b0604130633t48adb432n4417c3fd27f3b8f4@mail.gmail.com> > > > require "tk" > > #methods > > def guess > > if (guess.value =3D=3D right_guess.value) > > status.value =3D "Correct!" > > else > > number_of_guess.numeric +=3D 1 > > status.value =3D "Sorry, no luck" > > end > > end > > Probably, you'll want to refer local variables on main. > But those variables are out of scope in the method. > If arguments bothers you, you can use a Proc object instead of a method. > > Next, please see here. > > > def guess > ^^^^^ > > guess =3D TkVariable.new() > ^^^^^ > > There is conflict of names. > So, "proc{guess}" refers the local variable. > If you want to call "guess" method, you must use "proc{guess()}". > > > root =3D Tk.mainloop > > Probably, the return value is not useful. > > If I were you, I may write such like as the following. > ---------------------------------------------------- > require 'tk' > > right_guess =3D rand(21) > number_of_guess =3D TkVariable.new(0) > > Tk.root.title('Number guess') > > TkLabel.new(:textvariable=3D>number_of_guess).pack(:side=3D>:top, > :pady=3D>10) > > status =3D TkLabel.new(:text=3D>"Entry your guess and press the button", > :width=3D>37).pack(:side=3D>:bottom, :pady=3D>10) > > entry =3D TkEntry.new.pack(:side=3D>:top, :pady=3D>10) > > guess_cmd =3D proc{ > value =3D entry.value > if value.empty? || value.to_i !=3D right_guess > number_of_guess.numeric +=3D 1 > status.text('Sorry, no luck') > else > status.text('Correct!') > end > } > > button =3D TkButton.new(:text=3D>'Guess', > :command=3D>guess_cmd).pack(:side=3D>:top, :p\ady=3D>10) > > entry.bind('Return', proc{button.invoke}) > > Tk.mainloop > ---------------------------------------------------- > -- > Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp) > > -- "winners never quit, quitters never win" ------=_Part_35784_8290629.1145020606024--