Making entrys work with tkinter.

Tomasz Lisowski lisowski.tomasz at sssa.NOSPAM.com.pl
Wed Mar 28 06:59:13 EST 2001


Uzytkownik "Brian Elmegaard" <be at mek.dtu.dk> napisal w wiadomosci
news:3AC1CB21.4BD10EE8 at mek.dtu.dk...
> Hi group,
>
> I have been trying to find out how to use the an entry and grab the
> result. I am sorry, that I have not been able to figure out from
> different sources how bind the entry to an event and make it put back
> the result to the calling function.
> What I try is:
>
> class Sheetlabel:
>     def __init__(self,canvas,x,y):
>         self.frame=Toplevel(width=10,height=20)
>         self.frame.title('Insert label')
>         Label(self.frame,text='Label:').pack(side='left')
>         self.entry=Entry(self.frame)
>         #FIX THIS
>         self.frame.bind('<Return>',self.ok(x,y,canvas))
>         self.entry.pack(side='right')
>         self.frame.focus_set()
>
>     def ok(self,x,y,canvas,event=None):
>         self.text=self.entry.get()
>         self.frame.destroy()
>         canvas.create_text(20+x,20+y,text=self.text)
>         return self.text
>
> which should put the label text on the canvas. With a create_text in
> __init__ there is no problem. So somehow I think I am struggling with
> scopes. Is that right?

If I am not mistaken, even callbacks can take only one argument - the event
object. All other arguments must have default values. In your case, you want
to call a callback with x, y, and canvas, as arguments. This is not
supported.
Better store x, y, and canvas as instance attributes, and use them in the
ok() method.

Tomasz Lisowski





More information about the Python-list mailing list