以下程序使用一些按钮模拟交通灯系统。按钮显示正确,但如果我试图调用方法来创建/更改LED,它会以错误的方法结束。这是代码的重要部分:
class GUI (threading.Thread):
def __init__(self, num):
threading.Thread.__init__(self)
def run(self):
global window
window = Tk()
window.title('Ampel GUI')
window = Canvas(window, width=400, height=200)
window.pack()
button1 = Button(window, text="Press", command=lambda: pushbutton(25))
button1.pack()
button1.place(x=190, y=70)
button2 = Button(window, text="Press", command=lambda: pushbutton(12))
button2.pack()
button2.place(x=115, y=160)
(...)
button6 = Button(window, text="V2", command=lambda: pushbutton(22)) # V2
button6.pack()
button6.place(x=280, y=130)
window.mainloop()
@staticmethod
def output(self, lampe, status):
if status == 0:
if lampe == 21:
window.create_oval(140, 30, 160, 10, fill="#FFA6A6")
if lampe == 20:
window.create_oval(170, 30, 190, 10, fill="#FAFAAA")
callthread=GUI()
callthread=threading.Thread(target=GUI.output, args=(21,0))
callthread.start()
如何修复callthread-part,以便使用参数(21,0)调用输出方法?现在所有它最终都是
TypeError: __init__() takes exactly 2 arguments (1 given)
// edit:这是固定版本的样子: class GUI(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global window
window = Tk()
window.title('Ampel GUI')
window = Canvas(window, width=400, height=200)
window.pack()
button1 = Button(window, text="Press", command=lambda: pushbutton(25))
button1.pack()
button1.place(x=190, y=70)
button2 = Button(window, text="Press", command=lambda: pushbutton(12))
button2.pack()
button2.place(x=115, y=160)
(...)
button6 = Button(window, text="V2", command=lambda: pushbutton(22)) # V2
button6.pack()
button6.place(x=280, y=130)
window.mainloop()
@staticmethod
def output(lampe, status):
if status == 0:
if lampe == 21:
window.create_oval(140, 30, 160, 10, fill="#FFA6A6")
if lampe == 20:
window.create_oval(170, 30, 190, 10, fill="#FAFAAA")
callthread=GUI()
callthread=threading.Thread(target=GUI.output, args=(21,0))
callthread.start()
答案 0 :(得分:2)
您的错误就在这一行:
callthread=GUI()
问题是__init__
被定义为:
def __init__(self, num):
因此,要么在创建GUI
对象时提供参数,要么从num
方法中删除__init__
参数。
注意:此答案适用于您编辑前的代码。修改完成后,您从num
中删除了__init__
参数。您不应再使用已编辑的代码获取TypeError
。
答案 1 :(得分:0)
从num
删除__init__()
参数,从self
删除output()
参数,因为它是静态方法。
答案 2 :(得分:0)
可能你不应该制作静态方法,也不需要将其设为全局方法。
记住不要尝试子类化threading.Thread如果不清楚它是如何工作的。而是使用以下方法。没有子类分类线程,它将为您轻松完成所有事情。
试试这个:
class GUI()
def __init__(self):
window = Tk()
window.title('Ampel GUI')
self.window = Canvas(window, width=400, height=200)
self.window.pack()
button1 = Button(self.window, text="Press", command=lambda: pushbutton(25))
button1.pack()
button1.place(x=190, y=70)
button2 = Button(self.window, text="Press", command=lambda: pushbutton(12))
button2.pack()
button2.place(x=115, y=160)
(...)
button6 = Button(self.window, text="V2", command=lambda: pushbutton(22)) # V2
button6.pack()
button6.place(x=280, y=130)
window.mainloop()
def output(lampe, status):
if status == 0:
if lampe == 21:
self.window.create_oval(140, 30, 160, 10, fill="#FFA6A6")
if lampe == 20:
self.window.create_oval(170, 30, 190, 10, fill="#FAFAAA")
callthread=GUI()
callthread=threading.Thread(target=callthread.output, args=(21,0))
callthread.start()