2017-07-03 11 views
0

私はpython 3(tkinterを使用)でrubicsキューブタイマーに取り組んでいます。バインディングが動作しませんtkinter python3

私は私にスペースキーをバインドしようとしてい (もともと私は起動と停止のためにこれを実行しようとしましたが、私はあまりにもハードそれを見つけた)あなたがスペースを押すと、タイマーが を停止するようにそれを作るしようとしています関数を停止しますが、エラーを返します(バインドする関数の名前を付けた後に括弧を外すと、2つの引数を渡していると思っています)。 ここに私のコードは、Thxは、ソリューションのための事前です。

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

答えて

1

あなたは非常に近いです。

最初に、bind関数は、関数自体を引数として必要とします。最後に()があるので、この場合はNoneの関数を実行した結果が渡されます。

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

第2に、bindが呼び出す関数は、イベント引数を受け入れる必要があります。だから、このように定義する必要があります:

def Stop(self, event=None): 
+0

それは本当にありがとう –

関連する問題