Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的套接字通信器,C中的服务器_Python_Sockets_Tkinter_Communicator - Fatal编程技术网

python中的套接字通信器,C中的服务器

python中的套接字通信器,C中的服务器,python,sockets,tkinter,communicator,Python,Sockets,Tkinter,Communicator,我试图在Python3中为基于套接字的通信器添加一个图形界面 我正在使用服务器(工作正常)。我认为问题在于Tkinter mainloop()函数(第39行)。我把它搬到了很多不同的地方,似乎什么都不管用。没有Tkinter窗口,它工作得很好。顺便说一句,当我在没有Tkinter的情况下打开该通信器的两个实例时,消息正在发送和接收,Tkinter没有。 请帮帮我,我没什么主意了。 PS对不起,我的英语不好,它不是我的第一语言(Python也不是)。这段代码的一半似乎希望通过标准流与用户通信,而另

我试图在Python3中为基于套接字的通信器添加一个图形界面 我正在使用服务器(工作正常)。我认为问题在于Tkinter mainloop()函数(第39行)。我把它搬到了很多不同的地方,似乎什么都不管用。没有Tkinter窗口,它工作得很好。顺便说一句,当我在没有Tkinter的情况下打开该通信器的两个实例时,消息正在发送和接收,Tkinter没有。 请帮帮我,我没什么主意了。
PS对不起,我的英语不好,它不是我的第一语言(Python也不是)。这段代码的一半似乎希望通过标准流与用户通信,而另一半则希望使用Tk窗口。在您完全切换之前,一切都不会起作用,此时您的通信将发生在对
mainloop()
的调用中。此代码的一半似乎希望通过标准流与用户通信,而另一半则希望使用Tk窗口。在完全切换之前,一切都不会起作用,此时您的通信将发生在对
mainloop()
的调用中。
import sys
import socket
import select
from tkinter import *


def chat_client():
    # initialize GUI
    window = Tk()
    window.geometry("600x800")
    window.title("Communicator")

    w = Canvas(window, width=580, height=450, bg="ivory2")
    w.place(x=5, y=300)

    # whole conversation is stored here
    conversation = Text(window, width=80, height=30, wrap=WORD)
    conversation.place(x=10, y=310)
    label_enter = Label(window, text="Enter message: ")
    label_enter.place(x=5, y=20)

    # the message from here is being taken to send / write on std out
    message_entry = Entry(window, width=30)
    message_entry.place(x=120, y=20)

    msg = ""

    def say():
        x = message_entry.get()
        sys.stdout.write(x)
        conversation.insert(0.0, x + '\n')
        msg = message_entry.get().encode()
        message_entry.delete(0, 'end')


    btn_send = Button(window, text="Send!", command=say).place(x=120, y=50)

    #THIS SEEMS TO BE THE PROBLEM!!!
    window.mainloop()
    ######################

    host = 'localhost'
    port = 8888

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

    # connect to remote host
    try:
        s.connect((host, port))

    except:
        print('Unable to connect')
        sys.exit()

    print('Connected to remote host. You can start sending messages')
    #sys.stdout.write('[Me] ')
    sys.stdout.flush()

    while 1:

        socket_list = [sys.stdin, s]

        # Get the list sockets which are readable
        ready_to_read, ready_to_write, in_error = select.select(socket_list, [], [], 1)

        for sock in ready_to_read:
            if sock == s:
                # incoming message from remote server, s
                data = sock.recv(4096)
                if not data:
                    print('\nDisconnected from chat server')
                    sys.exit()
                else:
                    # print data
                    sys.stdout.write(str(data))
                    #conversation.insert(0.0, str(data) + '\n')
                    sys.stdout.write('\n-> ');
                    sys.stdout.flush()

            else:
                # user entered a message
                #msg = message_entry.get().encode() #sys.stdin.readline().encode()
                s.send(msg.encode())
                msg2 = sys.stdin.readline().encode()
                s.send(msg2)
                sys.stdout.write('\n-> ');
                sys.stdout.flush()
                message_entry.delete(0, 'end')


if __name__ == "__main__":
    sys.exit(chat_client())