gpt4 book ai didi

python - 将函数传递给 Python 3 中的类

转载 作者:太空宇宙 更新时间:2023-11-04 10:38:19 25 4
gpt4 key购买 nike

一旦我添加 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 模块的方法来实现这一点

最佳答案

您正在将函数转换为字符串:

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. 你在start中引用了timercondition不是self.timercondition;和
  2. timer 将永远运行,因为它永远不会返回控制权以允许您调用 stop

前者容易解决,后者更难解决。

关于python - 将函数传递给 Python 3 中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22247362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com