Hey guys, I got a little bored and thought I would to make some of the basic Client/Server applications I have a little bit flashier with a gui interface. So far I have just been working with the server and the main problem that I run into is when the program has to enter a loop while waiting for a response the gui starts to hang. The work around I have been using so far which quite frankly sucks, is to print a response while the exeption is being raised. It keeps the gui from hanging but it's quite annoying. The other thing I tried was to have it sleep for a short period of time but the gui seems to really hate that. Is there a more common or better way to handle this? Here is the code I'm working with at the moment, sorry I do relize it's pretty sloppy right now. I promise to clean it up a bit once I can get it functioning a little better :).
from tkinter import * from threading import Thread from time import sleep import socket class Gui(): def __init__(self, master): self.running = 0 #not listening self.addr = "" self.conn = "" self.frame = Frame(master) self.frame.pack(side = LEFT, anchor = N) self.startb = Button(self.frame, text = "Start", command = self.startc) self.startb.pack(side = LEFT, anchor = N) self.stopb = Button(self.frame, text = "Stop", command = self.stopc) self.stopb.pack(side = LEFT, anchor = N) self.connectionl = Label(self.frame, text = "not connected") self.connectionl.pack(side = LEFT, anchor = SW) def startc(self): if self.running == 0: self.running += 1 self.ls = socket.socket(socket.AF_INET, socket.SOCK_STREAM) port = 9999 self.ls.bind(('', port)) print("Server listening on port %s" %port) self.ls.listen(1) self.ls.settimeout(.05) while self.running != 0: try: (self.conn, self.addr) = self.ls.accept() print("client is at", self.addr, "on port", self.addr) self.running = 0 except: print("attempting to connect")#hangs without this if self.conn != "": self.rc = "" while self.rc != "done": try: self.rc = self.conn.recv(100).decode('utf-8') print("got command", self.rc) except: sleep(.05) print("no info sent yet")#hangs without this self.conn.close() else: self.running = 0 print("Server not listening") def stopc(self): self.running = 0 root = Tk() gui = Gui(root) root.mainloop()