Python function calls the wrong method/target

我的梦境 提交于 2019-12-13 09:47:03

问题


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

回答1:


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.




回答2:


Remove the num argument from __init__() and the self argument from output() since it is a static method.




回答3:


May be you should not make a static method and no need to make it global.

Remember dont try to subclass threading.Thread if are not clear about how it works. Instead use the following approach. It will do everything easy for you without sub classing thread.

try this:

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


来源:https://stackoverflow.com/questions/31388796/python-function-calls-the-wrong-method-target

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!