我正在 tkinter 中制作一个应用程序,它使用ttk.Scale
小部件来显示 mp3 歌曲的过程。我有一个功能,我想添加按钮的名称(按钮)应该依赖于文件名。因此我做了这个例子:
from tkinter import Tk, Button
from tkinter.filedialog import askopenfilenames
from tkinter.ttk import Scale
from threading import Timer
root = Tk()
slider = Scale(root, from_=0, to=100, orient='horizontal')
slider.pack()
# slider is continuously set to a bigger number so that it keeps going
def update_slider(num):
slider.set(num)
num += 1
root.after(50, update_slider, num)
update_slider(num=0)
# this function creates buttons based on the files opened
def add_buttons():
# the 'X' button of this particular window slows down execution of update_slider function
files = askopenfilenames(title='Add Buttons')
for i in list(files):
Button(root, text=i).pack()
button = Button(root, text='Browse', command=lambda: Timer(0.1, add_buttons).start())
button.pack()
root.mainloop()
我面临的问题是,当我打开 askopenfilenames 对话框或按下其“X”按钮时,在后台连续运行的滑块卡住了,因此无法正确显示进程。
这是一张我按住“X”按钮并ttk.Scale
停止移动的图片:
我尝试使用线程来运行该add_buttons
函数,但程序的行为保持不变。
我可以用类似的东西编辑 askopenfilenames 对话框,overrideredirect(True)
这样我就可以制作自己的标题栏和“X”按钮,并且生成的事件不会减慢我的 Scale?
回复:
我无法在 Linux 中重现该问题,无论我对文件对话框窗口做什么,比例都会不断变化。所以这可能是操作系统特定的问题。
我知道这个问题不会出现在 Linux 上。我在使用 root 的关闭按钮和其他 Toplevels 的关闭按钮时遇到了同样的问题,但我通过使用overrideredirect(True)
.
我可以用这个askopenfilenames
窗口做任何类似的事情吗?