|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
请教大神 我想设置 开始 结束 暂停 继续四个按钮 来控制 startpz() 这个函数的运行 请问怎么弄谢谢
def startpz()
for i in rows:
if .......
break
def star():
global cx
cx = True
startpz()
def stop():
top.destroy()
if __name__=="__main__":
import tkinter, time
top = tkinter.Tk()
cx = True
top.title("自动")
thelable=tkinter.Label(top,text="需要结束程序,请先按下空格暂停")
top.geometry('300x150') # Size 200, 200
startButton = tkinter.Button(top, height=2, width=20, text ="Start",
command = star)
stopButton = tkinter.Button(top, height=2, width=20, text ="Stop",
command = stop)
thelable.pack()
startButton.pack()
stopButton.pack()
top.mainloop()
本帖最后由 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()
复制代码
|
|