[Tutor] using the enter key

Michael Lange klappnase at freenet.de
Thu Apr 7 12:35:41 CEST 2005


On Thu, 07 Apr 2005 10:06:46 +1000
"Nova Nova" <nova585 at hotmail.com> wrote:

>     	def create_widgets(self):
>         	 self.a_lbl=Label(self,text="",fg="Red")
>         	 self.a_lbl.grid(row=0,column=0,columnspan=2,sticky=W)
>         	 self.inst_lbl=Label(self,text="Enter A Number Between 1 and 
> 100.",fg="blue")
>         	 self.inst_lbl.grid(row=1,column=0,columnspan=2,sticky=W)
>         	 self.pw_lbl=Label(self,text="Number: ")
>         	 self.pw_lbl.grid(row=2,column=0,sticky=W)
>         	 self.pw_ent=Entry(self)
>         	 self.pw_ent.grid(row=2,column=1,sticky=W)
>         	 self.submit_bttn=Button(self,text=str(self.g)+" Turns  
> Left",command=self.reveal,fg="red",bg="black")
>         	 self.submit_bttn.grid(row=3,column=0,sticky=W)
>         	 self.win_txt=Text(self,width=35,height=5,wrap=WORD)
>         	 self.win_txt.grid(row=4,column=0,columnspan=2,sticky=W)
> 
> 
> 	def submit(self):
>         	 self.reveal
>         	 entry.bind('<Return>',self.submit)
> 
> this is what im doing and i get an error, IT DOES NOT WORK
> 

The problematic part is your submit() method:

    def submit(self):
        self.reveal    # you forgot the parentheses here to call your reveal() method
                   ^^  
        entry.bind('<Return>',self.submit)

this only binds the event handler to the Entry when the submit button was pressed before;
you better apply this binding in your create_widgets() method immediately after creating the Entry
widget. Please note that if you want to use the submit() method both as button-command and as
event handler for the entry, it needs an optional event instance as argument:

    def submit(self, event=None):
        (...)

because the button's "command" doesn't pass an event to the callback, but the entry's event handler does.

I hope this helps

Michael


More information about the Tutor mailing list