TypeError:send_data()缺少1个必需的位置参数:' event'

时间:2017-09-20 13:53:21

标签: python sockets tkinter

这是代码

from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
import socket
import threading

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


class Chat(object):

    def __init__(self):

        self.IP = ''
        self.PORT = 9999
        self.conn()

        window = Tk()
        window.title("3406")
        frame = Frame(window).pack()

        ws = window.winfo_screenwidth()
        hs = window.winfo_screenheight()
        x = int((ws / 2) - (300 / 2))
        y = int((hs / 2) - (300 / 2))
        window.geometry('{}x{}+{}+{}'.format(500, 350, x, y))
        window.resizable(0, 0)

        self.text = Text(frame,yscrollcommand=True)
        self.text.pack(anchor=W)
        self.entry = Entry(frame)
        self.entry.pack(side=LEFT, expand=YES, fill=X, anchor=W)
        self.entry.focus_force()             
        self.entry.bind("<Return>", self.send_data)

        #self.button = Button(frame, text="发送", command=self.send_data).pack(side=RIGHT, anchor=W)



        self.send_threading = threading.Thread(target=self.send_data)
        self.recive_threading = threading.Thread(target=self.recive_data)
        self.send_threading.setDaemon(True)
        self.recive_threading.setDaemon(True) 
        self.send_threading.start()
        self.recive_threading.start()

        window.mainloop()

    def conn(self):
        try:
            s.connect((self.IP, self.PORT))
        except:
            messagebox.showerror("Error", "Error the client is not open")

    def recive_data(self):
        while True:
            data = s.recv(1024)
            data = data.decode("utf-8")
            print(data)
            self.text.insert(END,data + "\n")

    def send_data(self,event):
        self.event=event
        try:
            data = self.entry.get()
            # print(data)
            if data is not None:
                s.send(data.encode("utf-8"))
                self.entry.delete(0, 'end')

        except:
            messagebox.showerror("Error", "Error the client is not open!")
            exit(1)

if __name__ == '__main__':
    Chat()

我的错误是TypeError:send_data()缺少1个必需的位置参数:&#39; event&#39;  我是新人,所以我无法弄清楚我做错了什么。我不知道这意味着什么。

2 个答案:

答案 0 :(得分:2)

您的代码可能存在许多问题,但它不会按照您的预期执行操作。然而,这个错误恰恰意味着它所说的。 send_data需要一个参数,但您不会发送一个参数。

self.send_threading = threading.Thread(target=self.send_data)

在没有任何参数的情况下调用send_data函数。该函数需要event中的某些内容,但不会收到参数并引发异常。

答案 1 :(得分:0)

你需要添加你的函数event = None来纠正你的错误,即:def send_data(self,event = None):