2015-05-12 53 views
0

我正在製作一個項目,每當我停止秒錶時,它就會將時間更新爲一個Listbox。它可以在self.m listbox中更新。但它不能更新到topscoreslistbox如何在python中回調方法?

printFunctionStop函數中,我在類中調用Stop方法。但是我不知道如何在主要功能中調用TopscoresStop

Stop方法和TopscoresStop方法做同樣的事情。然而,不同的是一個在課堂上,一個在主要功能上。

那麼我該如何調用方法TopscoresStop中的主功能printFunctionStop誰知道?

class StopWatch(Frame): 
    def Stop(self):          
     """ Stop the stopwatch, ignore if stopped. """ 
     tempo = self._elapsedtime - self.lapmod2 
     if self._running: 
      self.after_cancel(self._timer)    
      self._elapsedtime = time.time() - self._start  
      self._setTime(self._elapsedtime) 
      self._running = 0 

     if len(self.laps) == 3: 
      return 

     self.laps.append(self._setLapTime(tempo)) 
     self.m.insert(END, (str(self.one.get()) , "%.2f" % self._elapsedtime)) 
     self.m.yview_moveto(1) 

def main(): 
    def Topscores(): 
     toplevel() = Toplevel() 
     toplevel.title("Top Scores") 
     topscoreslistbox = Listbox(toplevel, selectmode=EXTENDED, height=3, width=20, font=("Helvetica", 26))     
     topscoreslistbox.pack(side=RIGHT, fill=BOTH, expand=1, pady=5, padx=2) 

     first = Label(toplevel, text=("1st"), font=("Algerian", 30)) 
     first.pack(side=TOP) 

     second = Label(toplevel, text=("2nd"), font=("Algerian", 30)) 
     second.pack(side=TOP)  

     third = Label(toplevel, text=("3rd"), font=("Algerian", 30)) 
     third.pack(side=TOP)   

    def TopscoresStop(): 
     tempo = sw._elapsedtime - sw.lapmod2 
     if sw._running: 
      sw.after_cancel(sw._timer)    
      sw._elapsedtime = time.time() - sw._start  
      sw._setTime(sw._elapsedtime) 
      sw._running = 0 

     if len(sw.laps) == 3: 
      return 

     sw.laps.append(sw._setLapTime(tempo)) 
     topscoreslistbox.insert(END, (str(sw.one.get()) , "%.2f" % sw._elapsedtime)) 
     topscoreslistbox.yview_moveto(1) 

    def printFunctionStop(channel): 

     sw.event_generate("<<Stop>>", when = "tail")  

    GPIO.add_event_detect(16, GPIO.FALLING, callback = printFunctionStop, bouncetime=300) 
    sw.bind("<<Stop>>", lambda event:sw.Stop()) 

回答

1

您是否嘗試過將函數作爲參數傳遞給printfunctionstop

抱歉,無法發表評論。

E.g.

def a(): 
    print('hi') 

def b(func): 
    func() 

def main(): 
    b(a) 

main() 

主要應打印 'HI'