简体   繁体   English

Python TKinter 进度条 label 动作不正确

[英]Python TKinter progress bar label not acting correctly

I have really simple program with two progress bars in a window.我有一个非常简单的程序,在 window 中有两个进度条。 I try only update the first progress bar, but the second progress bars label updates as well, when it shouldn't.我尝试只更新第一个进度条,但第二个进度条 label 也会更新,但它不应该更新。

I am using j_4321's answer from this post on progress bar labels.我正在使用 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()

You get this weird behavior because you use the same layout.你会得到这种奇怪的行为,因为你使用相同的布局。 To fix this change the name of the second layout.要解决此问题,请更改第二个布局的名称。 In this case I added '1' to the name: 'text.Horizontal.TProgressbar1'在这种情况下,我在名称中添加了“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()

Both of your scrollbars are using the same layout named "text.Horizontal.TProgressbar".您的两个滚动条都使用名为“text.Horizontal.TProgressbar”的相同布局。 When you change that layout, it will change all widgets that use that layout.当您更改该布局时,它将更改使用该布局的所有小部件。

The fact that you're using style_1 and style_2 doesn't matter.您使用style_1style_2的事实并不重要。 What matters is the name of the layout you are configuring.重要的是您正在配置的布局的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM