我正在尝试使用套接字在Python中创建一个聊天应用程序。我无法连接(HOST,PORT)对。
这是GUI代码段,错误出现在client_socket.connect(ADDR)
行中:
root = Tk()
root.title("Kchat V1.0")
mainScreenlabel = Frame(root)
mainScreenlabel.pack()
mainScreeninput = Frame(root)
mainScreeninput.pack(side=BOTTOM)
HOST = StringVar()
titleLabel = Label(mainScreenlabel, text="Welcome to Kchat!!")
inputLabel = Label(mainScreeninput, text="Host IP :")
inputEntry = Entry(mainScreeninput, textvariable=HOST)
connectButton = Button(mainScreeninput, text="Connect", command=connect)
titleLabel.pack()
inputLabel.pack(side=LEFT)
inputEntry.pack(side=LEFT)
connectButton.pack()
PORT = 33000
BUFSIZ = 1024
ADDR = (HOST, PORT)
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect(ADDR)
receive_thread = Thread(target=receive)
receive_thread.start()
root.mainloop() #Starts GUI execution
这是我得到的错误:
Tracebak (most recent call last):
File".\chatClient.py", line 76, in <module>
client_socket.connect(ADDR)
TypeError: str, bytes or bytearray expected, not StringVar
答案 0 :(得分:0)
您可以尝试:
client_socket.connect(HOST, PORT)