I have a program that needs to open Toplevel
windows except the main Tk()
window in tkinter.
In the main window I have a Scale
widget which is updated every 100 miliseconds with the after
call. However in a state where the Toplevel window is open and the scale is updated when I press down the 'X' button in the Toplevel
window the Scale
stops moving.
This is my code:
from tkinter import Tk, Toplevel, Scale
root = Tk()
slider = Scale(root, orient='horizontal')
slider.pack()
num = 0
def main():
global num
slider.set(num)
num += 1
slider.after(500, main)
def toplevel():
win = Toplevel()
root.bind('<space>', lambda x: [main(), toplevel()])
root.mainloop()
When I stop pressing the 'X' button the Scale jumps to the point it should be
How can I keep the slider/scale flowing normally even when I hold down the 'X' button?
And also why does this happen?
Thanks in advance!
This issue would happen on Windows. Your code works fine on Linux.(I've tested it)
A possible reason is here:
This post also mentioned another solution: use thread.
Due to tkinter is single-threaded and those features are packaged, it seems using thread doesn't work in tkinter.
The cause is how operate system handle those "holding down" events on the title bar.
An easy solution is just hiding your title bar, and custom these buttons by yourself.(Avoid OS handling those events.) Like:
Binding some events or using some nice color makes your UI prettier.