本帖最后由 qq1151985918 于 2022-4-20 22:34 编辑
别的暂停可能还麻烦些,tkinter简单,只要弹出一个对话框阻塞住进程就好了
简单敲了一个样例
from tkinter import Tk, StringVar, Label, Button
from tkinter.messagebox import askokcancel
from threading import Thread
from time import sleep
class App(Tk):
def __init__(self):
super().__init__()
self.geometry('100x120')
self.resizable(0, 0)
self.switch = ''
self.set_label = StringVar()
Label(self, textvariable=self.set_label).pack()
Button(self, text='开始', command=self.run).pack()
Button(self, text='暂停', command=lambda: self.set_switch('pause')).pack()
Button(self, text='停止', command=lambda: self.set_switch('stop')).pack()
self.mainloop()
def run(self):
def _run():
x = 0
while True:
if self.switch == 'run':
sleep(0.2)
x += 1
self.set_label.set(str(x))
print(x)
elif self.switch == 'pause':
choose = askokcancel('提示:', '已暂停,点击确定继续,点击取消停止!')
if choose:
self.switch = 'run'
else:
self.switch = 'stop'
else:
return
self.switch = 'run'
t = Thread(target=_run)
t.setDaemon(True)
t.start()
def set_switch(self, x):
self.switch = x
if __name__ == '__main__':
app = App()
|