Как я могу прослушать нажатие кнопки в Tkinter? Мне нужно слушать кнопку, а не запускать функцию, но я хочу запустить функцию, которая прослушивает кнопку.
Обновление
Вот код:
#!/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)
## 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()