带有百分比标签的进度栏? [英] Progressbar with Percentage Label?
本文介绍了带有百分比标签的进度栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在显示百分比的进度条中间放置标签? 问题在于python不支持标签背景的透明性,所以我不知道该如何解决.
How can I put a label in the middle of a progressbar that shows the percentage? The problem is that python doesn't support transparency for label backgrounds, so I don't know how I can solve that.
推荐答案
使用ttk.Style
可以做到这一点.这个想法是修改Horizontal.TProgressbar
样式的布局(对于垂直进度条,与Vertical.TProgressbar
相同)以在栏中添加标签:
This is possible using a ttk.Style
. The idea is to modify the layout of the Horizontal.TProgressbar
style (do the same with Vertical.TProgressbar
for a vertical progressbar) to add a label inside the bar:
通常的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
更改标签的文本.
Then, the text of the label can be changed with 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()
这篇关于带有百分比标签的进度栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文