将函数传递给Python 3中的类

时间:2014-03-07 10:20:23

标签: python python-3.x

一旦我添加了 self.methodToRun = function ,它就不会显示任何错误  然而这个功能并没有被调用。(这就是发生的事情

>>> 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模块的情况下实现这个目标

1 个答案:

答案 0 :(得分: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. 您在timercondition中引用self.timercondition start;和
  2. timer将永远运行,因为它永远不会返回控件以允许您调用stop
  3. 前者很容易解决,后者则更不容易解决。