python tkinter 使用(六)
本文主要講述tkinter中進度條的使用。
1:確定的進度條
progressbar = tkinter.ttk.Progressbar(root, mode="determinate", maximum=100, value=0)
progressbar.pack()def updateProgressBar():for i in range(100):progressbar['value'] = i + 1root.update()time.sleep(0.1)button = tkinter.Button(root, text='Running', command=updateProgressBar)
button.pack()
-
ttk.Progressbar
來創建確定進度條; -
mode設置為:determinate;
-
設置
maximum
和value
屬性來控制進度條的進度。 -
root.update()來更新繪制頁面
2:不確定的進度條
progress = tkinter.ttk.Progressbar(root, length=200, mode="indeterminate", orient=tkinter.VERTICAL)
progress.pack()def start():# 開始進度條progress.start()def stop():#結束progress.stop()button = tkinter.Button(root, text='start', command=start)
button.pack(side=tk.LEFT)
button = tkinter.Button(root, text='stop', command=stop)
button.pack(side=tk.RIGHT)
3:自定義樣式的progressbar
通過ttk.Style
來自定義進度條的樣式,例如修改進度條的顏色、背景色。
#樣式自定義進度條
style = ttk.Style()
style.configure('red.Horizontal.TProgressbar', foreground='black', background='red')
custom = ttk.Progressbar(root, style='red.Horizontal.TProgressbar', mode='determinate', maximum=100, value=0)
custom.pack()# 更新進度條
def update():for i in range(100):custom['value'] = iroot.update()time.sleep(0.1)custom['value'] = 0# 創建按鈕
btn = tk.Button(root, text='StartStyle', command=update)
btn.pack()
red.Horizontal.TProgressbar
是一個基于Progressbar
的自定義樣式,用于創建一個水平方向的進度條,前景色和背景色都是紅色。