I'm working on an asynchronous TCP server.
All is going well, and I'm learning a lot from it, but I have a question related to the structure.
This is how the structure looks:
The Server object is an asynchronous TCP server running in its own thread. It communicates with the GUI Controller object via thread-safe queue's.
On the main thread we've got the GUI Controller , the tkinter mainloop and the actual Tkinter GUI class.
here is some code to make it more clear:
clientlist = []
#queue's for communicating with server thread
buffer = queue.Queue()
buffer2 = queue.Queue()
root = Tk()
#GUI controller
controller = Controller(buffer,buffer2,clientlist,root)
#Make Tkinter GUI and give a reference to the controller
top = MainWindow(controller, root)
#Giving Tkinter GUI reference to the controller
controller.set_top(top)
t1 = threading.Thread(target=run_server, args=(buffer,buffer2))
t1.start()
controller.check_connections()
root.protocol("WM_DELETE_WINDOW", on_closing)
try:
while controller.run:
root.mainloop()
print("[i] Joining server thread.")
t1.join()
I have a hard time finding a nice way to make the Tkinter GUI communicate with the GUI controller. The idea I had here to give a reference to each others class is not working:
If I give the MainWindow a reference to Controller, I need to save it to a variable in the constructor of MainWindow. Then I need to update that variable each loop which is very demanding.
How can I give a reference the correct way, or is there a better way to let these two classes communicate?
Thanks a lot!