from tkinter import *
from tkinter import ttk
def DOTEST_GUI():
GUI = Tk()
w = 1920
h = 1080
ws = GUI.winfo_screenwidth()
hs = GUI.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
GUI.geometry(f'{w}x{h} {x:.0f} {y:.0f}')
def start_p():
progress.start(5)
def stop_P():
progress.stop()
def print_cf(event = None):
import time
print('s')
start_p()
time.sleep(5)
stop_P()
B_TEST = ttk.Button(GUI, text = "test", width = 15, command = print_cf)
B_TEST.pack()
progress = ttk.Progressbar(GUI, orient = HORIZONTAL, length = 100, mode = 'indeterminate')
progress.pack(pady = 10)
GUI.bind("<Return>", print_cf)
GUI.focus()
GUI.mainloop()
DOTEST_GUI()
按照此代碼進度條運行不正常。
我試圖洗掉stop_P(),它在 5 秒后作業time.sleep(5)。
我希望它在stop_P()代碼前 5 秒開始運行進度。
uj5u.com熱心網友回復:
如果你想運行 aProgressbar一段時間然后停止它,你可以做這樣的事情。合并print_cf啟動和停止進度的功能并設定范圍為 5,中間休眠 1 秒然后停止Progressbar。把它放在一個執行緒上可以讓你做一些比睡一秒鐘和列印一些東西更耗時的事情。
from tkinter import *
from tkinter import ttk
import time
import threading
def DOTEST_GUI():
GUI = Tk()
w = 1920
h = 1080
ws = GUI.winfo_screenwidth()
hs = GUI.winfo_screenheight()
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
GUI.geometry(f'{w}x{h} {x:.0f} {y:.0f}')
def run_thread():
execute_thread = threading.Thread(target=print_cf)
execute_thread.start()
def print_cf(event = None):
progress.start()
print('s')
for i in range(5):
print(i)
time.sleep(1)
if i ==4:
progress.stop()
B_TEST = ttk.Button(GUI, text = "test", width = 15, command = run_thread)
B_TEST.pack()
progress = ttk.Progressbar(GUI, orient = HORIZONTAL, length = 100, mode = 'indeterminate')
progress.pack(pady = 10)
GUI.bind("<Return>", print_cf)
GUI.focus()
GUI.mainloop()
DOTEST_GUI()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/486922.html
