绑定不起作用 tkinter python3 [英] binding not working tkinter python3

查看:33
本文介绍了绑定不起作用 tkinter python3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在 python 3 中开发一个 rubics 立方体计时器(使用 tkinter).

so I'm working on a rubics cube timer in python 3 (using tkinter).

我正在努力使当你按下空格时,计时器停止(最初我尝试这样做来启动和停止,但我发现它太难了)我试图将空格键绑定到我的 Stop 函数,但它要么返回一个错误(当我在命名要绑定的函数后省略一组括号时,它出于某种愚蠢的原因认为我传递了 2 个参数.idk 为什么这个发生)或根本不起作用.这是我的代码,提前感谢解决方案.

I am trying to make it so that when you press space, the timer stops (originally i tried to do this for starting and stopping but i found it too hard) I have tried to bind the space key to my Stop function but it either returns an error (when I ommit a set of parenthesis after naming the function to bind to, it for some stupid reason thinks I'm passing 2 arguments. idk why this happens) or doesnt work at all. here is my code, Thx in advance for the solution.

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()

推荐答案

你们很亲近.

首先,bind 函数需要函数本身作为参数.由于您在末尾有 (),因此您正在传递运行该函数的结果,在本例中为 None.把那些关掉:

First, the bind function needs the function itself as an argument. Since you have the () on the end, you are passing the result of running the function, in this case None. Just leave those off:

root.bind("<space>",sw.Stop)

其次,bind 调用的函数必须接受一个事件参数.所以你需要像这样定义它:

Second, the function that bind calls must accept an event argument. So you need to define it like this:

def Stop(self, event=None):

这篇关于绑定不起作用 tkinter python3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆