python调用函数怎么错_Python函数调用了错误的方法/目标

The following program simulates a traffic light system with some buttons. The buttons appear correctly, but if I'm trying to call the method to create/change the LEDs, it ends up in the wrong method. Here's the important part of the code:

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()

How do I fix the callthread-part, so that the output method is called with the arguments (21,0)? Right now all that it ends up with is

TypeError: __init__() takes exactly 2 arguments (1 given)

//edit: This is how the fixed version looks like:

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()

解决方案

Your error is on this line:

callthread=GUI()

The problem is that __init__ was defined as:

def __init__(self, num):

So either provide an argument when creating a GUI object, or remove the num argument from the __init__ method.

note: This answer applies to the code prior to your edit. After your edit you removed the num argument from __init__. You should no longer get the TypeError with your edited code.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值