Jump to content
xisto Community
Sign in to follow this  
jedipi

[python]got A Problem In My Little Program No reaction when button was pressed

Recommended Posts

I am a newbie in Python.

After read some parts of 《OReilly - ProgrammingPython2ndEd.chm》,
I try to write a small problem as following.
But it is no reaction when I press the button.
Does anyone can tell me why?

from Tkinter import * # get widget classesclass Inputer(Frame): # subclass our GUIdef __init__(self, parent=None): # constructor method  Frame.__init__(self, parent)  self.pack()  ent = Entry(self)  ent.insert(0, 'Type words here') # set text  ent.pack(side=LEFT) # grow horiz  ent.focus() # save a click  ent.bind('<Return>', (lambda event: self.input(ent.get()))) # on enter key  widget = Button(self, text='Query', command=self.input(ent.get()))  widget.pack(side=RIGHT)def input(self,text):print textif __name__ == '__main__': Inputer().mainloop()

Share this post


Link to post
Share on other sites

I am a newbie in Python.

 

After read some parts of 《OReilly - ProgrammingPython2ndEd.chm》,

I try to write a small problem as following.

But it is no reaction when I press the button.

Does anyone can tell me why?

 

from Tkinter import * # get widget classesclass Inputer(Frame): # subclass our GUIdef __init__(self, parent=None): # constructor method  Frame.__init__(self, parent)  self.pack()  ent = Entry(self)  ent.insert(0, 'Type words here') # set text  ent.pack(side=LEFT) # grow horiz  ent.focus() # save a click  ent.bind('<Return>', (lambda event: self.input(ent.get()))) # on enter key  widget = Button(self, text='Query', command=self.input(ent.get()))  widget.pack(side=RIGHT)def input(self,text):print textif __name__ == '__main__': Inputer().mainloop()

<{POST_SNAPBACK}>


Python is sensitive to indent. I am not sure whether it is the reason. But , at least , these

run well on my gentoo box:

#!/usr/bin/env pythonfrom Tkinter import * # get widget classesclass Inputer(Frame): # subclass our GUI  def __init__(self, parent=None): # constructor method    Frame.__init__(self, parent)    self.pack()    ent = Entry(self)    ent.insert(0, 'Type words here') # set text    ent.pack(side=LEFT) # grow horiz    ent.focus() # save a click    ent.bind('<Return>', (lambda event: self.input(ent.get()))) # on enter key    widget = Button(self, text='Query', command=self.input(ent.get()))    widget.pack(side=RIGHT)  def input(self,text):    print textif __name__ == '__main__': Inputer().mainloop()

chmod u+x test.py

./test.py


Share this post


Link to post
Share on other sites

This is an old post but I must put it right.

The Button widget command attribute is not correct. It needs to be a callable, but what you are actually doing is calling it. self.input() gets called so you should see that the entry box text is printed to your output instantly without any action. This also means your button won't work, but the Enter key would.

self.input is referenced and is a callable which command calls itself when the associated action with the button is triggered. You also can't pass variables like this with the command attribute so in my code below I show you how you do this using lambda instead, with a few minor improvements, could have added more but I'd write you a whole application that may keep me occupied rather than answering this.

#!/usr/bin/env python# -*- coding: utf-8 -*-from Tkinter import *class Inputer(Frame):    def __init__(self, parent=None):	    Frame.__init__(self, parent)	    self.pack()	    ent = Entry(self)	    ent.insert(0, 'Type words here')	    ent.pack(side=LEFT)	    ent.select_range(0, END)	    ent.focus()	    ent.bind('<KP_Enter>', lambda event: self.input(ent.get()))	    ent.bind('<Return>', lambda event: self.input(ent.get()))	    widget = Button(self, text='Query', command=lambda: self.input(ent.get()))	    widget.pack(side=RIGHT)    def input(self, text):	    if text == 'Type words here':		    return	    print textif __name__ == '__main__':    Inputer().mainloop()

Cheers,


MC

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.