每次运行我的TK应用程序时,它都会冻结,相信这与Queue
模块有关。
我的代码:
import socket, os, multiprocessing, queue
import tkinter as tk
#---global variables---#
setup = ''
cleintsocket = ''
port = 0
q = queue.Queue()
#---Defs---#
def setup():
global host, port, user
host = setup_host_box.get()
port = setup_port_box.get()
user = setup_user_box.get()
if port == '':
port = 8545
create_sock(host, int(port))
def connect_buffer(self, hostname, connectingport):
connect(self, hostname, connectingport)
def connect(self, hostname, connectingport):
'''connects to a port'''
if hostname == '':
hostname = 'localhost'
self.connect((hostname, connectingport))
resvbackgroung = multiprocessing.Process(target = resv)
resvbackgroung.start()
chat()
def create_sock(nhost, nport):
'''create the cleint's socket'''
global cleintsocket
cleintsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connect(cleintsocket, nhost, nport)
def send(username, cleintsock):
'''to send a message'''
usrmsg = (username + ' - ' + chat_msg_box.get()).encode()
cleintsock.send(usrmsg)
def resv():
'''resive subscript, run through mutiprosses module'''
while True:
rmsg = cleintsocket.recv(1024).decode()
q.put(rmsg)
def chat():
'''loads chat page'''
setup_host_text.pack_forget()
setup_host_box.pack_forget()
setup_port_text.pack_forget()
setup_port_box.pack_forget()
setup_user_text.pack_forget()
setup_user_box.pack_forget()
setup_confirm_button.pack_forget()
chat_msg_display_text.pack()
chat_msg_box.pack()
chat_msg_send_button.pack()
chat_disconnect_button.pack()
while True:
while not q.empty():
msg = q.get()
chat_msg_display_text.insert('END.END', msg)
def start():
'''starts the setup page'''
setup_host_text.pack()
setup_host_box.pack()
setup_port_text.pack()
setup_port_box.pack()
setup_user_text.pack()
setup_user_box.pack()
setup_confirm_button.pack()
def disconnect():
'''safely closes the socket and sends a desconnect msg'''
cleintsocket.send((user + 'has disconnected').encode())
cleintsocket.close()
quit()
def send_button_callback():
'''add a buffer to allow time for 'cleintsocket' to be defined in 'create_sock()'''
send(user, cleintsocket)
#---TK Setup---#
#--window setup--#
window = tk.Tk()
window.title('Chat')
window.geometry('600x600')
window.configure(background='#ffffff')
#--connection setup page--#
setup_host_text = tk.Label(window, text = 'Host')
setup_host_box = tk.Entry(window, bg = '#ffffff')
setup_port_text = tk.Label(window, text = 'Port')
setup_port_box = tk.Entry(window, bg = '#ffffff')
setup_user_text = tk.Label(window, text = 'Username')
setup_user_box = tk.Entry(window, bg = '#ffffff')
setup_confirm_button = tk.Button(window,text = 'Connect', command = setup)
#--chat page--#
chat_msg_box = tk.Entry(window, bg='#ffffff')
chat_msg_send_button = tk.Button(window, text = 'send', command = send_button_callback)
chat_msg_display_text = tk.Text(window, width=600, height=20, insertborderwidth = 3, wrap = 'word')
chat_disconnect_button = tk.Button(window, text = 'Disconnect', command = disconnect)
#--------------#
start()
谢谢,
CB
答案 0 :(得分:0)
问题在于您的connect
功能:
您设置了多处理,将函数resv
作为刚刚生成的此过程的目标。
然后您开始此过程,并在此之后直接调用chat
,其中包含while True
循环。
这会阻止tkinter的主要事件线程。