How can I listen for a button press in Tkinter
How can I listen for a button press in Tkinter? I need to listen for a button and not run a function but, I want to run a function that listens for a button.
Update Here is the code:
#!/usr/bin/env python
import time
import thread
from Tkinter import *
class at(Frame):
def __init__(self, *args, **params):
## Standard heading: initialization
apply(Frame.__init__, (self,) + args, params)
self._parent = None
if len(args) != 0: self._parent = args[0]
self._init_before()
self.v = StringVar()
## Widget creation
self._widgets = {}
self._widgets['button#1'] = Button(self, name='button#1', text='up',)
self._widgets['button#1'].grid(column=1, row=1)
self._widgets['entry#1'] = Entry(self, name='entry#1', textvariable=self.v)
self._widgets['entry#1'].grid(column=2, row=1)
self._widgets['button#2'] = Button(self, name='button#2', text='down',)
self._widgets['button#2'].grid(column=1, row=2)
## Scroll commands
## Resize behavior(s)
self.grid_rowconfigure(1, weight=0, minsize=30)
self.grid_rowconfigure(2, weight=0, minsize=31)
self.grid_columnconfigure(1, weight=0, minsize=30)
self.grid_columnconfigure(2, weight=0, minsize=65)
self.pack(side=TOP, fill=BOTH, expand=1)
开发者_StackOverflow社区 ## Call to post-init method
self._init_after()
def _init_before(self):
self._init_specificBefore()
def _init_after(self):
self._init_specificAfter()
def u(self):
if self.listening==True:
self.u=True
self.listening=False
def d(self):
if self.listening==True:
self.d=True
self.listening=False
def listen(self):
#listen for self.u and self.d
def _init_specificBefore(self):
pass
def _init_specificAfter(self):
range=[0,100]
current_guess=range[1]/2
n=1
x=""
while 1:
self.v.set(str(current_guess)+"?")
x=self.listen()
if x=="u":
range[0]=current_guess
if range[0]==0:
current_guess=round(current_guess+round(range[1]/2))
else:
current_guess=round(current_guess+round((range[1]-range[0])/2))
elif x=="d":
range[1]=current_guess
if range[0]==0:
current_guess=round(round(range[1]/2))
else:
current_guess=range[0]+(round((range[1]-range[0])/2))
elif x=="y":
print "It took me "+str(n)+" guesses."
break
n=n+1
if __name__ == '__main__':
root = Tk()
o = at(root)
o.pack(side=TOP, fill=BOTH, expand=1)
root.mainloop()
What you are trying to accomplish is unclear, but it sounds like you misunderstand how GUI programming works in general and are just asking the wrong question. Generally speaking, If you think you need to poll for button presses rather than take advantage of the event loop you are doing it wrong.
So, the best answer to "how can I listen for a button and not run a function" is "you can't". It's a bit like asking "how can I put this screw in the wall with a hammer?". You can, but it's not the right way to use the tool so for all practical purposes you can't and shouldn't be taught how to.
It seems to me you are making a guessing game, and I think you want the user to press the up or down button after each guess. is that correct? If so, why not just have the up and down buttons call a function that makes a single guess?
You'll want to create a loop along the lines of while not exiting
where exiting is declared False at the start and is toggled when quitting the application. Then in that loop you can put functions that check certain state and update other state. Each time round the loop it will run each of these functions. The magic is to make each of these functions fast enough that it seems like they're running constantly.
精彩评论