带有百分比标签的进度栏?

如何在显示百分比的进度条中间放置标签?问题是python不支持标签背景的透明性,所以我不知道该如何解决。

回答:

使用可以做到这一点ttk.Style。这个想法是修改Horizontal.TProgressbar样式的布局(Vertical.TProgressbar对垂直进度条做同样的事情),在样式条中添加一个标签:

通常的Horizontal.TProgressbar布局:

[('Horizontal.Progressbar.trough',

{'children': [('Horizontal.Progressbar.pbar',

{'side': 'left', 'sticky': 'ns'})],

'sticky': 'nswe'})]

带有附加标签:

[('Horizontal.Progressbar.trough',

{'children': [('Horizontal.Progressbar.pbar',

{'side': 'left', 'sticky': 'ns'})],

'sticky': 'nswe'}),

('Horizontal.Progressbar.label', {'sticky': 'nswe'})]

然后,可以使用来更改标签的文本style.configure

这是代码:

import tkinter as tk

from tkinter import ttk

root = tk.Tk()

style = ttk.Style(root)

# add label in the layout

style.layout('text.Horizontal.TProgressbar',

[('Horizontal.Progressbar.trough',

{'children': [('Horizontal.Progressbar.pbar',

{'side': 'left', 'sticky': 'ns'})],

'sticky': 'nswe'}),

('Horizontal.Progressbar.label', {'sticky': ''})])

# set initial text

style.configure('text.Horizontal.TProgressbar', text='0 %')

# create progressbar

variable = tk.DoubleVar(root)

pbar = ttk.Progressbar(root, style='text.Horizontal.TProgressbar', variable=variable)

pbar.pack()

def increment():

pbar.step() # increment progressbar

style.configure('text.Horizontal.TProgressbar',

text='{:g} %'.format(variable.get())) # update label

root.after(200, increment)

increment()

root.mainloop()

结果的屏幕截图

以上是 带有百分比标签的进度栏? 的全部内容, 来源链接: utcz.com/qa/407992.html

回到顶部