2014-03-07 29 views
1

也不會一次我加self.methodToRun =功能 但功能不被調用顯示任何錯誤。(這是什麼情況在Python傳遞函數的一類3

>>> func() 
1 
>>> ================================ RESTART ================================ 
>>> 
>>> tt = timer_tick(1,func) 
>>> tt.start() 
>>>) 

這裏的代碼

import time 

def func(): 
    print('1') 

class timer_tick: 

    def __init__(self, num, function): 
     self.delay = num 
     self.methodToRun = function 
     self.timercondition = False 
    def start(self): 
     timercondition = True 
     self.timer() 
    def timer(self): 
     while self.timercondition: 
      self.methodToRun() 
      time.sleep(self.delay) 

    def stop(self): 
     timercondition = False 

def method1(): 
    return 'hello world' 

def method2(methodToRun): 
    result = methodToRun() 
    return result 

好,我發現這一點的同時寫一個秒錶程序,即定時效果可以通過tkinter.Tk.after()函數來實現。我能夠加入控制停止,暫停並用它重置定時器。

import tkinter 
import random 
import time 

frame = tkinter.Tk() 
frame.title("Stopwatch") 
frame.geometry('500x300') 

t='00:00.0' 
helv30 = ('Helvetica', 30) 
num = 0 
timer = 1 


def timerhand(): 
    global num,n 
    num += 1 
    formate(num) 
def formate(num): 
    global t,n 
    if (get_seconds(num) >= 0 and get_seconds(num)<10): 
    if (num%100)%10 == 0 and (num//600) < 10 : 
     t ='0'+ str(get_minutes(num)) + ':'+'0' + str(get_seconds(num))+'.0' 
    elif (num//600) < 10: 
     t ='0'+ str(get_minutes(num)) + ':'+'0' + str(get_seconds(num)) 
    elif (num%100)%10 == 0: 
     t =str(get_minutes(num)) + ':'+'0' + str(get_seconds(num))+'.0' 
    else: 
     t = str(get_minutes(num)) + ':'+'0' + str(get_seconds(num)) 
    else: 
    if (num%100)%10 == 0 and (num//600) < 10 : 
     t ='0'+ str(get_minutes(num)) + ':' + str(get_seconds(num))+'.0' 
    elif (num//600) < 10: 
     t ='0'+ str(get_minutes(num)) + ':' + str(get_seconds(num)) 
    elif (num%100)%10 == 0: 
     t =str(get_minutes(num)) + ':' + str(get_seconds(num))+'.0' 
    else: 
     t = str(get_minutes(num)) + ':' + str(get_seconds(num)) 
def get_minutes(num): 
    return (num//600) 
def get_seconds(num): 
    return (num%600)/10 

def stop(): 
    global timer,t,num 
    timer = 0 
    t = '00:00.0' 
    num = 0 
def pause(): 
    global timer,t 
    timer = 0 

def start(): 
    global timer 
    timer = 1 
    clock() 

canvas1 = tkinter.Canvas(frame,width = 300, height = 100,bg = 'black') 
t_message = canvas1.create_text(150,50, text = t , fill = 'blue', font = helv30) 

b1= tkinter.Button(frame,text = "Stop",command = stop) 
b1.pack(side ="bottom") 

b2= tkinter.Button(frame,text = "Start",command = start) 
b2.pack(side ="bottom") 

b2= tkinter.Button(frame,text = "Pause",command = pause) 
b2.pack(side ="bottom") 


#here is the time function implementation 

def clock(): 
    global canvas1,t_message 
    timerhand() 

    canvas1.itemconfig(t_message, state = 'hidden') 
    t_message = canvas1.create_text(150,50, text = t , fill = 'blue', font = helv30) 
    canvas1.pack()  

    if timer == True: 
     frame.after(100,clock) 


clock() 


canvas1.pack() 


frame.mainloop() 

所以我一直在尋找各種方法來實現這一目標沒有Tkinter的模塊

+0

你想要將該函數傳遞給該類中的哪些方法?你可以像普通的參數一樣傳球。 –

+0

'stop()'應該設置'timercondition = False'。 – glglgl

+0

而'self.timercondition:'應該夠了。 – glglgl

回答

4

你的函數轉換爲字符串:

self.methodToRun = str(function) 

因此,當你把它叫做:

self.methodToRun() 

您試圖調用一個字符串,這將不起作用。你可以只存儲功能就像任何其他參數:

self.methodToRun = function 

又通,就像任何其他的說法:

tt = timer_tick(my_num, my_func) 

你或許應該還可以存儲delaytimercondition爲實例屬性:

def __init__(self, num, function): 
    self.delay = num 
    self.methodToRun = function 
    self.timercondition = False 

編輯:更新後的版本有兩個問題:

  1. 您參考timerconditionself.timercondition,在start;和
  2. timer將永遠運行,因爲它永遠不會返回控制權以允許您撥打stop

前者容易解決,後者則少得多。

+0

以及它不顯示任何錯誤,一旦我添加 – nabeel

+0

我編輯了我的答案,以反映您的編輯問題。爲了將來的參考,「它沒有顯示任何錯誤,一旦我添加」是*不*有用的評論。 – jonrsharpe

+0

對不起。在我意識到這個評論太大而不能發表評論之前,我在評論框中輸入內容,但是我在剪輯中剪切了所有內容,可能沒有看到它。 – nabeel