-2
我試圖在蟒蛇插座/ Tkinter的圖形聊天室,但我得到一個錯誤套接字錯誤的「沒有定義
完全錯誤:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Theta\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "D:\CODEDRIVE_BACKUP\PyTkinter\1.2.py", line 19, in f
s.send(q)
NameError: name 's' is not defined
這裏是主機代碼:
from tkinter import *
import socket, time
tk = Tk()
def c():
d = a.get()
print('[LOG] NOW HOSTING ' + d)
HOST = d # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen(10)
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
time.sleep(1)
data = conn.recv(1024)
a = Entry(tk, width=55, bg='light green')
a.grid(row=1, column=1)
b = Button(tk, width=55, text="HOST", command=c)
b.grid(row=2, column=1)
tk.mainloop()
客戶端代碼:
from tkinter import *
import socket
import socket
tk = Tk()
def c():
d = a.get()
HOST = d # The remote host
PORT = 50007 # The same port as used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
def f():
q = open('SEND.txt', 'rb')
s.send(q)
data = s.recv(1024)
a = Entry(tk, width=55, bg='light green')
a.grid(row=1, column=1)
b = Button(tk, width=55, text="CONNECT", command=c)
b.grid(row=2, column=1)
c = Button(tk, width=55, text="SEND", command =f)
c.grid(row=3, column=1)
tk.mainloop()
在'def f():'...什麼是's'?你的錯誤不是在說謊 –
這個錯誤信息很清楚:你從來沒有定義過「s」,所以's.send(q)'沒有任何意義。 – Novel
變量也可以比單個字母多。請儘量讓您的代碼對其他人可以理解 –