如何將進度條連線到 Tkinter 中的函式?


進度條有助於視覺化正在執行的程序的狀態。我們已經使用並與許多進度條進行了互動,例如獲取從網際網路下載檔案的進度、在本地系統上載入檔案,等等。

假設我們希望在應用程式中建立進度條並連線它。我們將使用 ProgressBar(win, options) 方法建立一個全寬進度條。可以透過啟用和停用它的按鈕來配置它。

示例程式碼

#Import the required Libraries
from tkinter import *
from tkinter import ttk
import time

#Create an instance of tkinter frame
win= Tk()

#Set the geometry
win.geometry("750x250")

#Define a function to Show a Progress Bar
#Create a ProgressBar in a function
def run_progress():
   my_progress= ttk.Progressbar(win, orient= HORIZONTAL, length= 500, mode= 'determinate')
   my_progress['value']+=500
   my_progress.pack(pady=40)
   button.config(state= "disable")

#Create a Button
button=ttk.Button(win, text= "Run",command= run_progress)
button.place(x= 340, y= 100)

win.mainloop()

現在執行上述程式碼以顯示包含進度條的視窗。

輸出

在給定的輸出中,如果單擊“執行”按鈕,將開始執行進度條。

更新於: 04-May-2021

2K+ 瀏覽

啟動你的 職業生涯

完成課程以獲得認證

開始
廣告