Python 3.x 绑定不起作用tkinter蟒蛇3
因此,我正在使用Python3(使用tkinter)开发rubics多维数据集计时器 我试图使它,以便当你按下空格键时,计时器停止 (最初我尝试这样做是为了启动和停止,但我发现太难了) 我曾尝试将空格键绑定到Stop函数,但它要么返回错误(当我在命名要绑定的函数后使用一组括号时,出于某些愚蠢的原因认为我传递了2个参数。idk为什么会发生这种情况),要么根本不起作用。 这是我的代码,Thx提前解决方案Python 3.x 绑定不起作用tkinter蟒蛇3,python-3.x,tkinter,tk,Python 3.x,Tkinter,Tk,因此,我正在使用Python3(使用tkinter)开发rubics多维数据集计时器 我试图使它,以便当你按下空格键时,计时器停止 (最初我尝试这样做是为了启动和停止,但我发现太难了) 我曾尝试将空格键绑定到Stop函数,但它要么返回错误(当我在命名要绑定的函数后使用一组括号时,出于某些愚蠢的原因认为我传递了2个参数。idk为什么会发生这种情况),要么根本不起作用。 这是我的代码,Thx提前解决方案 from tkinter import * import time class StopWat
from tkinter import *
import time
class StopWatch(Frame):
""" Implements a stop watch frame widget. """
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self._start = 0.0
self._elapsedtime = 0.0
self._running = 0
self.timestr = StringVar()
self.makeWidgets()
def makeWidgets(self):
""" Make the time label. """
l = Label(self, textvariable=self.timestr)
self._setTime(self._elapsedtime)
l.pack(fill=X, expand=NO, pady=2, padx=2)
def _update(self):
""" Update the label with elapsed time. """
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._timer = self.after(50, self._update)
def _setTime(self, elap):
""" Set the time string to Minutes:Seconds:Hundreths """
minutes = int(elap/60)
seconds = int(elap - minutes*60.0)
hseconds = int((elap - minutes*60.0 - seconds)*100)
self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds))
def Start(self):
""" Start the stopwatch, ignore if running. """
if not self._running:
self._start = time.time() - self._elapsedtime
self._update()
self._running = 1
def Stop(self):
""" Stop the stopwatch, ignore if stopped. """
if self._running:
self.after_cancel(self._timer)
self._elapsedtime = time.time() - self._start
self._setTime(self._elapsedtime)
self._running = 0
print("fsddaewSDGNFHRAW") # a test to see if it works
def Reset(self):
""" Reset the stopwatch. """
self._start = time.time()
self._elapsedtime = 0.0
self._setTime(self._elapsedtime)
def main():
root = Tk()
sw = StopWatch(root)
root.bind("<space>",sw.Stop()) # this is where i tried to bind
# if i did this:
#root.bind("<space>",sw.stop) it would say im passing 2 parameters instead of one (self)
sw.pack(side=TOP)
Button(root, text='Start', command=sw.Start).pack(side=LEFT)
Button(root, text='Stop', command=sw.Stop).pack(side=LEFT)
Button(root, text='Reset', command=sw.Reset).pack(side=LEFT)
Button(root, text='Quit', command=root.quit).pack(side=LEFT)
root.mainloop()
if __name__ == '__main__':
main()
从tkinter导入*
导入时间
类别秒表(框架):
“”“实现秒表框小部件。”“”
def __;初始功率(自身,父项=无,**kw):
帧。\uuuuu初始化(自、父、千瓦)
自启动=0.0
self.\u elapsedtime=0.0
自运行=0
self.timestr=StringVar()
self.makeWidgets()
def makeWidgets(self):
“”“制作时间标签。”“”
l=标签(self,textvariable=self.timestr)
self.\u设置时间(self.\u elapsedtime)
l、 封装(填充=X,扩展=NO,pady=2,padx=2)
def_更新(自):
“”“用经过的时间更新标签。”“”
self.\u elapsedtime=time.time()-self.\u start
self.\u设置时间(self.\u elapsedtime)
self.\u定时器=self.after(50,self.\u更新)
def_设置时间(自身、elap):
“”“将时间字符串设置为分钟:秒:百分之一百”“”
分钟=整数(elap/60)
秒=整数(elap-分钟*60.0)
hs秒=整数((elap-分钟*60.0-秒)*100)
self.timestr.set(“%02d:%02d:%02d%”(分、秒、秒))
def启动(自):
“”“启动秒表,如果正在运行则忽略。”“”
如果不是自运行:
self.\u start=time.time()-self.\u elapsedtime
自我更新()
自运行=1
def停止(自):
“”“停止秒表,如果停止则忽略。”“”
如果自行运行:
取消后的自我(自我计时器)
self.\u elapsedtime=time.time()-self.\u start
self.\u设置时间(self.\u elapsedtime)
自运行=0
打印(“fsddaewSDGNFHRAW”)#测试是否有效
def重置(自):
“”“重新设置秒表。”“”
self.\u start=time.time()
self.\u elapsedtime=0.0
self.\u设置时间(self.\u elapsedtime)
def main():
root=Tk()
sw=秒表(根)
root.bind(“,sw.Stop())#这是我试图绑定的地方
#如果我这样做:
#root.bind(“,sw.stop)它会说我传递了2个参数,而不是一个(self)
开关组件(侧面=顶部)
按钮(root,text='Start',command=sw.Start).pack(side=LEFT)
按钮(root,text='Stop',command=sw.Stop).pack(side=LEFT)
按钮(root,text='Reset',command=sw.Reset).pack(side=LEFT)
按钮(root,text='Quit',command=root.Quit).pack(side=LEFT)
root.mainloop()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
你非常接近
首先,bind
函数需要函数本身作为参数。由于最后有()
,因此传递的是运行函数的结果,在本例中为None
。别再提了:
root.bind("<space>",sw.Stop)
def Stop(self, event=None):