Python function calls the wrong method/target

2024/10/6 9:33:11

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 windowwindow = 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)) # V2button6.pack()button6.place(x=280, y=130)window.mainloop()@staticmethoddef 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 windowwindow = 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)) # V2button6.pack()button6.place(x=280, y=130)window.mainloop()@staticmethoddef 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()
Answer

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.

https://en.xdnf.cn/q/120468.html

Related Q&A

How to make a triangle of xs in python?

How would I write a function that produces a triangle like this:xxxxxxxxxx xxxxxLets say the function is def triangle(n), the bottom row would have n amount of xsAll I know how to do is make a box:n = …

Pip freeze --local

I am following a video tutorial and that guy did this:$ pip freeze --local > requirement.txt $ cat requirement.txtthis is to export all these packages with their versions in another project, but how…

How to optimize this Pandas code to run faster

I have this code to create a swarmplot from data from a DataFrame:df = pd.DataFrame({"Refined__Some_ID":some_id_list,"Refined_Age":age_list,"Name":name_list …

i was creating a REST api using flask and while i was about to test it on postman I saw that error

File "c:\Users\kally\rest\code\app.py", line 3, in <module>from flask_jwt import JWTFile "C:\Users\kally\AppData\Roaming\Python\Python310\site-packages\flask_jwt\__init__.py",…

Web scrape get drop-down menu data python

I am trying to get a list of all countries in the webpage https://www.nexmo.com/products/sms. I see the list is displayed in the drop-down. After inspecting the page, I tried the following code but I m…

TypeError(unsupported operand type(s) for ** or pow(): str and int,)

import mathA = input("Enter Wright in KG PLease :") B = input("Enter Height in Meters Please :")while (any(x.isalpha() for x in A)):print("No Letters Please")A = input(&qu…

How can I do assignment in a List Comprehension? [duplicate]

This question already has answers here:How can I do assignments in a list comprehension?(8 answers)Closed 1 year ago.Generally, whenever I do a for loop in python, I try to convert it into a list comp…

KeyError: column_name

I am writing a python code, it should read the values of columns but I am getting the KeyError: column_name error. Can anyone please tell me how to fix this issue. import numpy as np from sklearn.clust…

Find starting and ending indices of list chunks satisfying given condition

I am trying to find the start and stop indices of chunks of positive numbers in a list.cross = [7,5,8,0,0,0,0,2,5,8,0,0,0,0,8,7,9,3,0,0,0,3,2,1,4,5,0,0,0,7,5] For the given example input, the desired o…

How can I Scrape Business Email Contact with python?

this morning I wanted to create a little Software/Script in Python, it was 6am when I started and now Im about to become crazy because its 22pm and I have nothing that works.So basically, I want to do …