[英]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.