Python 对象不可调用-线程
我试图构建一个窗口来控制我的程序,但当我执行它时:Python 对象不可调用-线程,python,python-3.x,multithreading,Python,Python 3.x,Multithreading,我试图构建一个窗口来控制我的程序,但当我执行它时: Exception in thread Thread-1: Traceback (most recent call last): File "D:\python3.8\lib\threading.py", line 932, in _bootstrap_inner self.run() File "D:\python3.8\lib\threading.py", line 870, in r
Exception in thread Thread-1:
Traceback (most recent call last):
File "D:\python3.8\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "D:\python3.8\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
TypeError: 'MainWindow' object is not callable
我不确定是语法错误还是其他原因
代码:
IDE:VS Code您应该有一个init函数,您当前没有调用对象构造函数,因此您没有实际的对象。尝试添加init函数
class MainWindow:
def __init__(self):
self.start()
def quit(self)->'mine':
windows.destroy()
exit()
def start(self):
self.frame = tk.Frame(windows)
self.frame.pack(side=tk.BOTTOM)
self.button = tk.Button(self.frame, text='quit', fg='red', command=quit)
self.button.pack(side=tk.RIGHT)
windows.mainloop()
t = threading.Thread(target = MainWindow())
t.start()
不能在线程中调用类。可以在线程中调用类的函数。首先创建类的对象,然后调用start函数。t=threading.Thread(target=obj.start())
class MainWindow:
def __init__(self):
self.start()
def quit(self)->'mine':
windows.destroy()
exit()
def start(self):
self.frame = tk.Frame(windows)
self.frame.pack(side=tk.BOTTOM)
self.button = tk.Button(self.frame, text='quit', fg='red', command=quit)
self.button.pack(side=tk.RIGHT)
windows.mainloop()
t = threading.Thread(target = MainWindow())
t.start()