'tkinter button press sometimes block thread execution
I have a button in tkinter that execute a function in a loop while the button is pressed like this:
self.APRTE_button = tk.Button(self, text="Button", width=20, height=2,
command=lambda: self.button_hold_callback('APRTE'))
self.APRTE_button.bind('<Button-1>',
lambda event: self.button_hold_callback('APRTE', event))
self.APRTE_button.bind('<ButtonRelease-1>', self.button_stop_callback)
The two functions are
def button_hold_callback(self, *args):
global repeat
self.ser_send_text.delete('1.0', tk.END)
try:
repeat = self.after(450, self.button_hold_callback, args[0], args[1])
except IndexError:
pass
self.ser.write(DATA_TO_WRITE)
def button_stop_callback(self, event):
self.ser.reset_output_buffer()
self.ser.write(b'\x02\x56\xff\xff\xff\xff\xff\xff\x32\x35\x04')
logging.info("Written stop on the serial")
self.ser.close()
self.after_cancel(repeat)
I'm using a tkinter with a modified loop as below
def mainloop(self):
with ThreadPoolExecutor() as executor:
future = executor.submit(self._do_update_screen_loop)
try:
return super().mainloop()
finally:
# letting the thread know we're done
self._terminating.set()
with self._update_scheduled:
self._update_scheduled.notify_all()
def update_screen(self):
with self._update_scheduled:
self._update_scheduled.notify_all()
self.after(110, self.update_screen)
def _do_update_screen_loop(self):
while True:
with self._update_scheduled:
self._update_scheduled.wait()
if self._terminating.is_set():
return
self._do_data_screen()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
For some reason, when I kept clicked the button for long time or repetitive click it a lot of time, the _do_data_screen() function, which the only thing to do is listening to the serial port, basically stop executing. I know it because I know data are coming on the serial but the doesn't print something
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
