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
문제는 귀하의 connect
기능에 있습니다.
다중 resv
처리를 설정하고 방금 생성 한이 프로세스의 대상으로 함수 를 넣습니다 .
그런 다음 프로세스를 시작하고 루프 chat
가있는 바로 다음에 호출 while True
합니다.
이것은 tkinter의 메인 이벤트 스레드를 차단합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다