Tkinter gui与Python中的客户机-服务器

Tkinter gui与Python中的客户机-服务器,python,tkinter,Python,Tkinter,我使用Tkinter创建了一个简单的gui,客户端服务器正在运行。但是,无论出于什么原因,我都可以让客户机服务器进行通信。有人能看看我的代码并告诉我为什么它不工作吗 服务器: import Tkinter as tk import os import socket # Required to allow network communicate import re w, h = 500, 200 connection = None def key(self, event): self

我使用Tkinter创建了一个简单的gui,客户端服务器正在运行。但是,无论出于什么原因,我都可以让客户机服务器进行通信。有人能看看我的代码并告诉我为什么它不工作吗 服务器:

import Tkinter as tk
import os
import socket # Required to allow network communicate
import re
w, h = 500, 200
connection = None



def key(self, event):
    self.frame.focus_force()
    connection.send(event.keysym) # Sends the command to the robot



def SendEnterCommand(event): # Bound to the Enter key
    connection.send("enter") # Sends the command to the robot

    print('Sent Message - Enter') # Feedback for the controller

def SendSpaceCommand(event): # Bound to the Space key
    connection.send("space") # Sends the command to the robot

    print('Sent Message - Space')# Feedback for the controller

def SendKillCommand(event): # Bound to the Escape key
    connection.send("kill") # Sends the command to the robot

    print('Sent Message - Kill')# Feedback for the controller



# Used to check if the IP address follows the correct format
def CheckIP(IP):
    pat = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$") # Check if the ip address value inputted follows the form of an ip address
    test = pat.match(IP)

    if test:
       return True
    else:
       return False

def UpdateConnection(IPAddress, Port):

    currentCommand = "empty"

    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Initialise the socket

    serversocket.bind((IPAddress, Port)) # Set up the socket on YOUR IP address and selected port

    serversocket.listen(5) # Listen on the socket, up to 5 backlogged connections

    print('waiting for a connection')

    connection, client_address = serversocket.accept() # Wait for a connection, when there is one accept it

    print ('Connected with ' + client_address[0] + ':' + str(client_address[1])) # Print who we are connected to

    data = ""   

    while data != 'Kill': # Loop until the escape key is pressed
        root.update()

def SetUpConnection():
    IPAddress = IPEntry.get()
    Port = int(PortEntry.get())

    if CheckIP(IPAddress) and isinstance( Port, int ) and Port > 0 and Port < 9999:  # check if the ip address is of the correct format, check if the port is an integer and greater than 0   
        #if InputMethodSelection.get() >= 1 and InputMethodSelection.get() <= 4: # check if there is a valid selected input option
           print( "Connecting", "Connecting To Server!")
           UpdateConnection(IPAddress, Port) # Connect and run the server          
        #else:
           # print( "ERROR", "Select an input type!")
    else:
        tkMessageBox.showinfo( "ERROR", "Invalid IP address or port")

# Add a couple widgets. We're going to put pygame in `embed`.
root = tk.Tk()
embed = tk.Frame(root, width=w, height=h)
embed.pack()

BroadcastBTN = tk.Button(root, text='Broadcast', command=SetUpConnection)
BroadcastBTN.pack()

tk.Label(root, text="IP:").pack(anchor = "w")

IPEntry = tk.Entry(root)
IPEntry.config(width=20)
IPEntry.pack(anchor = "w")


tk.Label(root, text="Port:").pack(anchor = "w")
PortEntry = tk.Entry(root)
PortEntry.config(width=10)
PortEntry.pack(anchor = "w")

# Show the window so it's assigned an ID.
root.update()
root.bind("<Key>", key)


root.bind("<Return>", SendEnterCommand) 
root.bind("<space>", SendSpaceCommand)
root.bind("<Escape>", SendKillCommand)
root.mainloop()

当你写“我可以让客户机-服务器通信”时,你真的是说不能吗?是否有例外情况?您确定要在服务器中输入9559以便客户端可以连接到它吗?如果是,它可能是系统上的防火墙;这是什么操作系统?
import socket # Required to allow network

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Initialise the socket

print('Connecting') # user guidance output

sock.connect(("192.168.1.59", 9559))



data = ""
print('I AM CONNECTED') # user guidance output 
while data != 'Kill': # Loop until the computer tries to close the connection
    data = sock.recv(1024) # Recieve data from the connection

    if(data == "EnterPressed"):
        print("Enter Pressed")
    elif(data == "space"):
        print("Space Pressed")
    elif(data == "forward"):
        print("Forward Pressed")
    elif(data == "left"):
        print("Left Pressed")
    elif(data == "right"):
        print("Right Pressed")
    elif(data == "backward"):
        print("Backward Pressed")
    elif(data == "pressed"):
        print("pressed Pressed")
    else:
        print(data)

sock.close()  
print('DISCONNECTED') # user guidance output