0

I'm trying to make a client-server package into one app. So when the app is open the server automatically goes into standby/listen mode. The issue is when running the server socket it doesn't show the gui until after socket.accept(). i would like the gui and the server socket to happen at the same time.

I have commented out lines of code and it works as intended up to the point of conn,addr=s.accept()

imports*

root= Tk()

#main config
#mouse drag
# ------------------ Server Socket ------------------
connectionStatus = Label(rootCanvasBG)
connectionStatus.grid(column=0, row=7)

s = socket.socket()
host = socket.gethostname()
port = 8080
s.bind((host, port))
s.listen(10)
print(host)
print('waiting for connection')
connectionStatus.config(text='Waiting for any incoming connections')
conn, addr = s.accept()  # thread issue, not showing tkinter
connectionStatus.config(text=addr)
print(addr, 'connected')

#Layout

root.mainloop()

I have read about threading to resolve this but no examples I have seen gave clear enough answers. I am still fairly new to python and programming in general. Thanks

randbo
  • 36
  • 1
  • 7
  • Have you looked at the answers to [Trying to fix tkinter GUI freeze-ups (using threads)](https://stackoverflow.com/questions/53525746/trying-to-fix-tkinter-gui-freeze-ups-using-threads)? – martineau Jun 02 '19 at 19:06

1 Answers1

0
import threading   # <<< Import Threading

# ------------------ Server Socket ------------------

connectionStatus = Label(rootCanvasBG)
connectionStatus.grid(column=0, row=7)

s = socket.socket()
host = socket.gethostname()
port = 8080
s.bind((host, port))
s.listen(10)
print(host)
print('waiting for connection')
connectionStatus.config(text='Waiting for any incoming connections')
def mainloop():   # <<< create a def
    while True:
        conn, addr = s.accept()  # thread issue, not showing tkinter
        connectionStatus.config(text=addr)
        print(addr, 'connected')
threading.Thread(target=mainloop).start() # <<< run loop on new thread

Found the solution, start by importing threading create a def mainloop() with a while loop for conn, addr = s.accept() the use threading.Thread() to target the mainloop and .start() to execute threading.

randbo
  • 36
  • 1
  • 7