使用可以做到这一点
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 tkfrom tkinter import ttkroot = tk.Tk()style = ttk.Style(root)# add label in the layoutstyle.layout('text.Horizontal.TProgressbar', [('Horizontal.Progressbar.trough', {'children': [('Horizontal.Progressbar.pbar', {'side': 'left', 'sticky': 'ns'})], 'sticky': 'nswe'}), ('Horizontal.Progressbar.label', {'sticky': ''})])# set initial textstyle.configure('text.Horizontal.TProgressbar', text='0 %')# create progressbarvariable = 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()


