Reputation: 35
This is my server script when running it without GUI the start method only it works fine but when running the whole script and I press the start button it just freezes and not responding and nothing appears on the text window
from Tkinter import *
import socket
import sys
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.text = Text(self, width = 35, height = 5, wrap = WORD)
self.text.grid(row = 0, column = 0, columnspan = 2, sticky = W)
self.submit_button = Button(self, text='start', command = self.start)
self.submit_button.grid(row = 2, column = 0, sticky = W)
def start(self):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.text.insert(0.0, 'Server started!\n' )
self.s.bind(('',1080))
self.s.listen(10)
while True:
sc, address = self.s.accept()
i=1
f = open('file_'+ str(i)+".txt",'wb') #open in binary
i=i+1
while (True):
l = sc.recv(1024)
while (l):
print l
f.write(l)
f.flush()
f.close()
sc.close()
root = Tk()
root.title("Server")
root.geometry("500x250")
app = Application(root)
root.mainloop()
Upvotes: 0
Views: 251
Reputation: 386210
The only way for a GUI to work is if its event loop is able to service events such as those that respond to requests to redraw itself, respond to buttons, etc. In Tkinter, this event loop is mainloop
.
When you click the start button you run no less than two infinite loops inside this event loop, which essentially freezes the event loop as long as these inner infinite loops are running.
Upvotes: 1