[英]Python TKinter progress bar label not acting correctly
我有一个非常简单的程序,在 window 中有两个进度条。 我尝试只更新第一个进度条,但第二个进度条 label 也会更新,但它不应该更新。
我正在使用 j_4321 在进度条标签上的帖子中的答案。
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
# Progress bar #1
style_1 = ttk.Style(root)
style_1.layout('text.Horizontal.TProgressbar',
[('Horizontal.Progressbar.trough',
{'children': [('Horizontal.Progressbar.pbar',
{'side': 'left', 'sticky': 'ns'})],
'sticky': 'nswe'}),
('Horizontal.Progressbar.label', {'sticky': ''})])
style_1.configure('text.Horizontal.TProgressbar', text='0 %')
variable_1 = tk.DoubleVar(root)
pbar_1 = ttk.Progressbar(root, style='text.Horizontal.TProgressbar', variable=variable_1)
pbar_1.pack()
# Progress bar #2
style_2 = ttk.Style(root)
style_2.layout('text.Horizontal.TProgressbar',
[('Horizontal.Progressbar.trough',
{'children': [('Horizontal.Progressbar.pbar',
{'side': 'left', 'sticky': 'ns'})],
'sticky': 'nswe'}),
('Horizontal.Progressbar.label', {'sticky': ''})])
style_2.configure('text.Horizontal.TProgressbar', text='0 %')
variable_2 = tk.DoubleVar(root)
pbar_2 = ttk.Progressbar(root, style='text.Horizontal.TProgressbar', variable=variable_2)
pbar_2.pack()
def increment():
# This dosn't change pbar_2's value, as it should act.
pbar_1.step()
# Why does this style_1.configure also change the text from style_2 as well???
style_1.configure('text.Horizontal.TProgressbar', text='{:g} %'.format(variable_1.get()))
root.after(200, increment)
increment()
root.mainloop()
你会得到这种奇怪的行为,因为你使用相同的布局。 要解决此问题,请更改第二个布局的名称。 在这种情况下,我在名称中添加了“1”: 'text.Horizontal.TProgressbar1'
style_2 = ttk.Style(root)
style_2.layout('text.Horizontal.TProgressbar1',
[('Horizontal.Progressbar.trough',
{'children': [('Horizontal.Progressbar.pbar',
{'side': 'left', 'sticky': 'ns'})],
'sticky': 'nswe'}),
('Horizontal.Progressbar.label', {'sticky': ''})])
style_2.configure('text.Horizontal.TProgressbar1', text='0 %')
variable_2 = tk.DoubleVar(root)
pbar_2 = ttk.Progressbar(root, style='text.Horizontal.TProgressbar1', variable=variable_2)
pbar_2.pack()
您的两个滚动条都使用名为“text.Horizontal.TProgressbar”的相同布局。 当您更改该布局时,它将更改使用该布局的所有小部件。
您使用style_1
和style_2
的事实并不重要。 重要的是您正在配置的布局的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.