这是我到目前为止的代码,理想情况下,我希望它暂停,直到用户再次单击开始。任何帮助表示赞赏!非常感谢:) <3
from tkinter import *
from tkinter import ttk
import time
from threading import *
import threading
import _thread
def start():
w.start()
def stop():
time.sleep(10)
def stopfunc():
_thread.start_new_thread(stop,())
window = Tk()
w = ttk.Progressbar(window)
w.pack()
button1 = ttk.Button(window, text = 'start', command = start)
button1.pack()
button2 = ttk.Button(window, text = 'stop', command = stopfunc)
button2.pack()
window.mainloop()
将
variable
参数设置为控制变量,例如 IntVar
。
from tkinter import *
from tkinter import ttk
def start():
w.start()
def stopfunc():
w.stop()
#var.set(var.get())
def reset():
var.set(0)
window = Tk()
var = IntVar()
w = ttk.Progressbar(window, variable=var)
w.pack()
button1 = ttk.Button(window, text = 'start', command = start)
button1.pack()
button2 = ttk.Button(window, text = 'stop', command = stopfunc)
button2.pack()
button2 = ttk.Button(window, text = 'reset', command = reset)
button2.pack()
window.mainloop()
如果您需要进度条与循环并发,这里有一种方法可以做到这一点:
from tkinter import *
from tkinter import ttk
ttl_ticks = 77 # your countable range
current_tick = 0
pause = True
def start_pause():
global current_tick
global pause
pause = not pause
while current_tick < ttl_ticks:
if not pause:
if current_tick == 0:
w.start()
w['value'] = current_tick / ttl_ticks * 100
w.update()
w.after(60)
current_tick += 1
if current_tick == ttl_ticks:
w.stop()
w['value'] = 100
current_tick = 0
pause = True
break
else:
w.stop()
w['value'] = current_tick / ttl_ticks * 100
break
def reset():
global current_tick
global pause
w.stop()
pause = True
current_tick = 0
window = Tk()
w = ttk.Progressbar(window, mode='determinate', length=180)
w.pack()
button1 = ttk.Button(window, text='start / pause', command=start_pause)
button1.pack()
button2 = ttk.Button(window, text='reset', command=reset)
button2.pack()
window.mainloop()