CustomTkinter messaging app using socket and threading slowing down once threading and server start #2552
Replies: 4 comments 7 replies
-
Try to put all GUI callbacks in different threads. |
Beta Was this translation helpful? Give feedback.
-
No worries. By default, neither So, in such applications, we need to use Threads explicitly. Here are two code examples, Problem Example and Solution Example to demonstrate it better that without Threading it holds the GUI window, while using Threads, provides smoother experience. Problem example: from time import sleep
from customtkinter import CTk, CTkButton
def send_msg():
send_button.configure(text="Sending...", state="disabled")
send_button.update()
sleep(8)
send_button.configure(text="Send", state="normal")
if __name__ == "__main__":
app = CTk()
app.geometry("500x300")
send_button = CTkButton(app, text="Send", command=send_msg)
send_button.place(relx=0.5, anchor="center", rely=0.5)
app.mainloop() Solution Example: from time import sleep
from threading import Thread
from customtkinter import CTk, CTkButton
def send_msg():
send_button.configure(text="Sending...", state="disabled")
send_button.update()
sleep(8)
send_button.configure(text="Send", state="normal")
def send_msg_thread():
Thread(target=send_msg).start()
if __name__ == "__main__":
app = CTk()
app.geometry("500x300")
send_button = CTkButton(app, text="Send", command=send_msg_thread)
send_button.place(relx=0.5, anchor="center", rely=0.5)
app.mainloop() In the above two codes, first Problem example will create discrepancies and will pause or stuck the window. And, second Solution Example will run smoothly and correctly. Hope this may be helpful to you. If there is anything else you want to have, mention here. |
Beta Was this translation helpful? Give feedback.
-
Here is a video I took showing both the old and new GUI and how the application starts lagging when the server is started only on the new GUI using customtkinter. (I had to zip it because github would not allow me to just select the video if you want it another way just let me know). |
Beta Was this translation helpful? Give feedback.
-
In the file messagingServer.py, the issue of a slow GUI response can be addressed with the following modifications: In the def startServer():
global server
global serverRunning
global clientNumChange
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverIp = ...
print(serverIp)
port = 443
server.bind((serverIp, port))
server.listen(1)
clientTetheringThread = threading.Thread(target= clientTethering)
clientTetheringThread.start()
print("Online")
while serverRunning:
if clientNumChange == True:
currentUserText.configure(state= "normal")
currentUserText.delete("1.0",customtkinter.END)
for i in range(len(clients)):
currentUserText.insert(customtkinter.END, "\n" + str(clients[i].id))
for j in range(len(rooms)):
currentUserText.insert(customtkinter.END, "\n" + str(rooms[j][0]))
currentUserText.configure(state= "disabled")
clientNumChange = False
sleep(0.1) # 100 milliseconds delay
print("Killed")
server.close() Along with this change, it's recommended to set the def startServerThread():
global serverRunning
serverRunning = True
serverThread = threading.Thread(target= startServer, daemon=True)
serverThread.start() These changes address the slow response issue in customtkinter, which is often due to its higher resource usage for rendering elements like rounded corners in Also, in Hope, above description would be helpful to you. What do you expect more, please mention here. |
Beta Was this translation helpful? Give feedback.
-
So when I implemented a new GUI using customtkinter (replacing tkinter and ttk), my server and client both run very slowly when moving the application window and when updating widget info such as text. after they start their own threads (the client connecting to the server, and the server starting the server). All my threads are set up correctly and do not cause any problems with the old GUI. Wondering if there are any known problems witth customttkinter and threads.
Beta Was this translation helpful? Give feedback.
All reactions