一起执行两个无限循环。 1用于多线程服务器的循环和用于GUI的其他无限循环。
没有显示GuI窗口。
我试图同时执行服务器和GUI,现在GUI窗口没有打开。
以下是代码。
import socket
import sys
from thread import *
from Tkinter import *
import tkMessageBox
blue ="#000fff000"
root = Tk()
frame = Frame(root)
frame.pack()
root.geometry("500x500")
e=NONE
frame=Frame(root)
frame.pack()
text_area = Text(frame)
text_area.pack(side=BOTTOM)
B1 = Button(root, text = "CLOSE", command = sys.exit)
B1.pack(side =TOP)
print "Hello"
try:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
except socket.error,msg:
print "Socket Creation Error"
sys.exit();
print 'Socket Created'
host = ''
port = 9008
try:
s.bind((host, port))
except socket.error,msg:
print "Bind Failed";
text_area.insert(END,"Bind failed")
sys.exit()
text_area.insert(END,"Socket bind complete")
s.listen(10)
text_area.insert(END,"Socket now listening")
a = []
def clientthread(conn):
global text_area
i=1024
while True:
data = conn.recv(1024)
text_area.insert(END,"\n")
text_area.insert(END,data)
i=i+1
text_area.insert(END,"\n")
reply = "Reply"+str(i)
text_area.insert(END,reply)
conn.send(reply)
print "hello"
print "bye"
while True:
conn, addr = s.accept()
start_new_thread(clientthread,(conn,))
conn.close()
s.close()
答案 0 :(得分:0)
您需要为套接字服务器的主循环启动另一个线程。这将允许主线程运行UI。
只需用以下代码替换循环:
def server_thread(s):
while True:
conn, addr = s.accept()
start_new_thread(clientthread,(conn,))
start_new_thread(server_thread,(s,))