Я пытаюсь создать простую «чат-комнату», в которой я не знаю, сколько сообщений я получу от клиента, но мой сервер получит их все, отобразит их и в конечном итоге ответит.Проблема в том, что когда я нажимаю на текстовое поле ответа, мой код падает.Я действительно не уверен, где моя проблема, и любая помощь будет работать.
from Tkinter import *
import tkMessageBox
import socket
import threading
# Global variables
SendConfirmation = "0"
ConnectionConfirmation = "0"
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
data_old = ""
# Initiates socket and connection
def ConnectBind():
global ConnectionConfirmation
global sock
server_address = (IP_Entry.get(), int(Port_Entry.get()))
sock.bind(server_address)
tkMessageBox.showinfo("Bind Successful", "Connected to " + IP_Entry.get() + " at port " + Port_Entry.get())
ConnectionConfirmation = "1"
# Sets the disconnect signal
def DisconnectBind():
global ConnectionConfirmation
ConnectionConfirmation = "2"
# Sets the Send Signal
def SendMessage():
global SendConfirmation
SendConfirmation="1"
# Running background listen
def BackgrounListen():
global data_old
data = connection.recv(1024)
if data != None:
if data != data_old:
Message_Text.insert('1.0', data + '\n')
# Window set up
root = Tk()
root.title('TCP/IP software')
ConnectionFrame = Frame(root)
ConnectionFrame.pack(side=TOP, fill=X)
SendFrame = Frame(root)
SendFrame.pack(side=TOP, fill=X)
MessageFrame = Frame(root)
MessageFrame.pack(side = BOTTOM)
# Connection information frame
IP_Label = Label(ConnectionFrame, text="IP address: ")
IP_Entry = Entry(ConnectionFrame, bd=10, text="Enter IP address here")
Port_Label = Label(ConnectionFrame, text="Port number: ")
Port_Entry = Entry(ConnectionFrame, bd=10, text="Enter port number here")
IP_Label.grid(row=0, column=0)
IP_Entry.grid(row=0, column=1)
Port_Label.grid(row=1, column=0)
Port_Entry.grid(row=1, column=1)
# Connect and bind to the address and port
Connect_Button = Button(ConnectionFrame, text="Connect", command=ConnectBind)
Connect_Button.grid(row=3, column=0)
DisConnect_Button = Button(ConnectionFrame, text="Disconnect", command=DisconnectBind)
DisConnect_Button.grid(row=3, column=1)
# Send messages frame
SendMessage_Entry = Entry(SendFrame, bd=10, text="Type your message here")
SendMessage_Button = Button(SendFrame, text="Send Message", command=SendMessage)
SendMessage_Entry.pack()
SendMessage_Button.pack()
# Information/Messages display frame
Message_Text = Text(MessageFrame, height=8, width=50)
Message_Scroll = Scrollbar(MessageFrame)
Message_Text.pack(side=LEFT, fill=Y)
Message_Scroll.pack(side=RIGHT, fill=Y)
Message_Scroll.config(command=Message_Text.yview())
Message_Text.config(yscrollcommand=Message_Scroll.set)
# Start the GUI before running
root.update_idletasks()
root.update()
#Working out main
Message_Text.insert(INSERT, "Software started")
while ConnectionConfirmation != "2":
if ConnectionConfirmation == "1":
sock.listen(1)
connection, client_address = sock.accept()
Message_Text.insert('1.0', "Client connected\n")
connection.sendall("connected to server")
root.update_idletasks()
root.update()
while ConnectionConfirmation == "1":
if connection.recv#:
# if SendConfirmation == "1":
# connection.send(SendMessage_Entry.get())
# SendConfirmation = "0"
background_thread = threading.Thread(target=BackgrounListen())
background_thread.daemon = True
background_thread.start()
root.update_idletasks()
root.update()
elif ConnectionConfirmation == "2":
sock.close()
#Message_Text.insert('1.0', "Socket properly closed")
#ConnectionConfirmation = "0"
#root.update_idletasks()
#root.update()
root.update_idletasks()
root.update()