I created a programm on python to read serial data from an arduino and show it on a tkinter window. I made a Thread to read the from the arduino and the tkinter programm. My Programm runs perfectly on windows but the problem is that i want to use it on my ubuntu laptop and there it doesn't work. It works ok but there are some problems. I have some sensor values that update if some new information comes from the arduino, but the values are not shown. Also and most importend no buttons work and I can't close the window.
My Programm has this structure:
|--Main.py (for starting the programm)
|--App.py (for the application)
| |--arduino
| | |--serial_reader.py (the Thread to read from my arduino)
| | |--Input_manager.py and save_Input.py (for saving the data in variebles)
| |--managers
| | |--button_manager.py
| | |--image_manager.py
| | |--text_manager.py (to manage the widgets of the ui)
| |--ui
| | |--custom_button.py
| | |--custom_image.py
| | |--custom_text.py (for creating/deleting/updating the widgets)
I tried narrowing down of the problem and the programm worked perfectly fine without this funktion in the custom_text class.
def delete_Text(self):
if self.anzahl == 1:
self.canvas.delete(self.tag)
else:
for i in range(0, self.anzahl):
self.canvas.delete(self.tag + str(i))
I don't see why this crashes the entire window. In the app I have a loop with the after funktion in tkinter:
def loop(self):
try:
while not data_queue.empty():
data = data_queue.get_nowait()
self.serial_Reader.read_Serial_Values(data)
self.text_manager.update()
except queue.Empty:
print("leer")
pass
self.window.after(1, self.loop)
It looks if the Thread got some information, then it will save this information in specific Variables and then the text should update.
In the text_manager the update function just calls th update function in the custom_ text file. which looks like this:
def update_Text(self):
self.delete_Text()
self.create_Text()
And that starts the delete_Text function which crashes the programm. I don't understand what is wrong with this function. I mean it worked and it doesn't show any error idk, but it's logic that the text is not updating, because I update the text with deleting and creating a new text. Hopfully somebody can help me
The problem probably isn’t with your delete_Text() function itself, but more with how you're using threads with tkinter.
In tkinter, you can’t call methods like canvas.delete
, canvas.create_text
, label.config
, and so on, from a thread other than the main thread.
I had a similar issue myself — everything worked fine on Windows, but on Linux, tkinter behaved differently, like it was stricter or something. And yeah, the app would freeze or the window just wouldn’t respond, exactly like what you’re seeing.
So, most likely, the issue is thread-related, not with the logic of your function.