我有一个由 python tkinter 编写的简单 ui,它只包含一个按钮。
我在这里发现了一个问题,如果按钮命令指向一个函数,其中包括创建一个实例来执行它的方法。但是,当我运行这个程序时,我的 pycharm 告诉我我正在向该方法传递一个位置参数,而我从未这样做过:
类型错误:tell_time() 采用 0 个位置参数,但给出了 1 个
由于某些原因,我必须将方法保留在类中。谁能告诉我如何让该方法运行?太感谢了!
def build_ui():
root = Tk()
root.title("Auto Hedger")
root.geometry("640x480")
btn1 = Button(root, text="get data", command=testing1)
btn1.pack()
root.mainloop()
class test_object():
def tell_time():
print(datetime.datetime.now())
def testing1():
aaa = test_object()
t1000 = Thread(target=aaa.tell_time, args=[])
t1000.start()
if __name__ == '__main__':
t_root = Thread(target=build_ui)
t_root.start()
您的tell_time
方法需要self
作为参数,因为它是类方法而不是函数。添加它应该使它工作正常。试试这个:
from threading import Thread
from tkinter import *
import datetime
def build_ui():
root = Tk()
root.title("Auto Hedger")
root.geometry("640x480")
btn1 = Button(root, text="get data", command=testing1)
btn1.pack()
root.mainloop()
class test_object():
def tell_time(self):
print(datetime.datetime.now())
def testing1():
aaa = test_object()
t1000 = Thread(target=aaa.tell_time, args=[])
t1000.start()
if __name__ == '__main__':
t_root = Thread(target=build_ui)
t_root.start()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句