Server app freezes with Tkinter GUI

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shaf3e
    New Member
    • Apr 2013
    • 2

    Server app freezes with Tkinter GUI

    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



    Code:
    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(('',1090))
            self.s.listen(10)
            sc, address = self.s.accept()
            self.text.insert(0.0, 'Got conn from', address )
            while True:
    
                '''msg = sc.recv(1024)
                print address, ' >> ', msg
                msg = raw_input('SERVER >> ')
                sc.send(msg)'''
                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()
                        l = sc.recv(1024)
                f.close()
    
                sc.close()
            
            #s.close()
    
    
    root = Tk()
    root.title("Server")
    root.geometry("500x250")
    app = Application(root)
    
    root.mainloop()
    Last edited by Rabbit; Apr 23 '13, 09:40 PM. Reason: Please use code tags when posting code.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Add a print statement at the beginning of start() and another after the socket statement to see if that is the hang-up, as the Tkinter code appears to work ok. Note that the inner "while True" statement never exits so the outher while True will only execute once.

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      It appears that Tkinter requires an update_idletask s() because of all of the other processing going on.
      Code:
               self.text.insert(0.0, 'Server started!\n' )
               self.text.update_idletasks()
      
               self.text.insert(0.0, 'Got conn from', address )
               self.text.update_idletasks()
      The hang-up now appears to be this line
      Code:
               sc, address = self.s.accept()

      Comment

      • shaf3e
        New Member
        • Apr 2013
        • 2

        #4
        i am new to socket programming ana i saw that the problem with accepting the connection but it was working well before buiding the GUI i dont know what to do

        Comment

        Working...