Home > Net >  Threading error: get() takes 0 positional arguments but 1 was given
Threading error: get() takes 0 positional arguments but 1 was given

Time:05-03

I have an issue with threading, the question was already asker but it didn't answer mine. I'm trying to create a chat app with socket to understand how it works better. The problem here is i can't get it to receive messages automatically, i would basically have to invoke the get() function each time i get a new message. I tried to fix that with threading but now i get an other error:

from tkinter import *
import threading
import socket
 
try:
    s = socket.socket()
except:
    print("Failed socket creation")     

port = 12345       


s.connect(('127.0.0.1', port))
print("Successfuly Connected")
    
    
    
    
def Main():
    m = tkinter.Tk()
    m.title('TrollChat(Client)')
    m.iconbitmap("troll.ico")
    m.geometry("400x400")

    def sendinput():
        message = e1.get()
        s.send(message.encode())

    tkinter.Label(m, text="Message: ").grid(row=0)
    e1 = tkinter.Entry(m)
    e1.grid(row=0, column=1)

    button1 = tkinter.Button(m, text='Send', width=25, command=sendinput)
    button1.grid(row=0, column=3)
 
    m.mainloop()
    
def get():
    while True:
        data = s.recv(1024).decode()
        if not data:
            pass
        print(data)
    
    



if __name__ == '__main__':
    
    t1 = threading.Thread(target=Main, args=(0,))
    t1.start()
    
    t2 = threading.Thread(target=get, args=(0,))
    t2.start()

The error is:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "E:\Bots\Thonny\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "E:\Bots\Thonny\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
TypeError: Main() takes 0 positional arguments but 1 was given

Exception in thread Thread-2:
Traceback (most recent call last):
  File "E:\Bots\Thonny\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "E:\Bots\Thonny\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
TypeError: get() takes 0 positional arguments but 1 was given

Other problem: I tried solving the issue by replacing

t1 = threading.Thread(target=Main, args=(0,)) and t2 = threading.Thread(target=get, args=(0,))

with

t1 = threading.Thread(target=Main(), args=(0,)) and t2 = threading.Thread(target=get(), args=(0,))

but it stops compiling after the first thread. Can anyone help with this issue please?

CodePudding user response:

To resolve your error, change the following block of code:

if __name__ == '__main__':
    
    t1 = threading.Thread(target=Main, args=(0,))
    t1.start()
    
    t2 = threading.Thread(target=get, args=(0,))
    t2.start()

to the following, since both Main() and get() do not accept any arguments:

if __name__ == '__main__':
    
    t1 = threading.Thread(target=Main)
    t1.start()
    
    t2 = threading.Thread(target=get)
    t2.start()
  • Related