Threading error: get() takes 0 positional arguments but 1 was given
Threading error: get() takes 0 positional arguments but 1 was given
我有线程方面的问题,这个问题已经有人提出了,但没有回答我的问题。我正在尝试使用套接字创建一个聊天应用程序以了解它如何更好地工作。这里的问题是我无法让它自动接收消息,每次收到新消息时我基本上都必须调用 get() 函数。我试图用线程解决这个问题,但现在我得到了另一个错误:
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()
错误是:
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
其他问题:
我尝试通过替换
来解决问题
t1 = threading.Thread(target=Main, args=(0,))
和
t2 = threading.Thread(target=get, args=(0,))
和
t1 = threading.Thread(target=Main(), args=(0,))
和
t2 = threading.Thread(target=get(), args=(0,))
但它在第一个线程后停止编译。有人可以帮忙解决这个问题吗?
要解决您的错误,请更改以下代码块:
if __name__ == '__main__':
t1 = threading.Thread(target=Main, args=(0,))
t1.start()
t2 = threading.Thread(target=get, args=(0,))
t2.start()
以下内容,因为 Main() 和 get() 都不接受任何参数:
if __name__ == '__main__':
t1 = threading.Thread(target=Main)
t1.start()
t2 = threading.Thread(target=get)
t2.start()
我有线程方面的问题,这个问题已经有人提出了,但没有回答我的问题。我正在尝试使用套接字创建一个聊天应用程序以了解它如何更好地工作。这里的问题是我无法让它自动接收消息,每次收到新消息时我基本上都必须调用 get() 函数。我试图用线程解决这个问题,但现在我得到了另一个错误:
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()
错误是:
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
其他问题: 我尝试通过替换
来解决问题t1 = threading.Thread(target=Main, args=(0,))
和
t2 = threading.Thread(target=get, args=(0,))
和
t1 = threading.Thread(target=Main(), args=(0,))
和
t2 = threading.Thread(target=get(), args=(0,))
但它在第一个线程后停止编译。有人可以帮忙解决这个问题吗?
要解决您的错误,请更改以下代码块:
if __name__ == '__main__':
t1 = threading.Thread(target=Main, args=(0,))
t1.start()
t2 = threading.Thread(target=get, args=(0,))
t2.start()
以下内容,因为 Main() 和 get() 都不接受任何参数:
if __name__ == '__main__':
t1 = threading.Thread(target=Main)
t1.start()
t2 = threading.Thread(target=get)
t2.start()