鱼C论坛

 找回密码
 立即注册
查看: 4318|回复: 3

[已解决]请教关于tkinter添加开始 结束 暂停 继续四个按钮控制程序运行

[复制链接]
发表于 2022-4-20 14:51:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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()
最佳答案
2022-4-20 22:32:46
本帖最后由 qq1151985918 于 2022-4-20 22:34 编辑

别的暂停可能还麻烦些,tkinter简单,只要弹出一个对话框阻塞住进程就好了
简单敲了一个样例

  1. from tkinter import Tk, StringVar, Label, Button
  2. from tkinter.messagebox import askokcancel
  3. from threading import Thread
  4. from time import sleep


  5. class App(Tk):
  6.     def __init__(self):
  7.         super().__init__()
  8.         self.geometry('100x120')
  9.         self.resizable(0, 0)
  10.         self.switch = ''
  11.         self.set_label = StringVar()
  12.         Label(self, textvariable=self.set_label).pack()
  13.         Button(self, text='开始', command=self.run).pack()
  14.         Button(self, text='暂停', command=lambda: self.set_switch('pause')).pack()
  15.         Button(self, text='停止', command=lambda: self.set_switch('stop')).pack()
  16.         self.mainloop()

  17.     def run(self):
  18.         def _run():
  19.             x = 0
  20.             while True:
  21.                 if self.switch == 'run':
  22.                     sleep(0.2)
  23.                     x += 1
  24.                     self.set_label.set(str(x))
  25.                     print(x)
  26.                 elif self.switch == 'pause':
  27.                     choose = askokcancel('提示:', '已暂停,点击确定继续,点击取消停止!')
  28.                     if choose:
  29.                         self.switch = 'run'
  30.                     else:
  31.                         self.switch = 'stop'
  32.                 else:
  33.                     return
  34.         self.switch = 'run'
  35.         t = Thread(target=_run)
  36.         t.setDaemon(True)
  37.         t.start()

  38.     def set_switch(self, x):
  39.         self.switch = x


  40. if __name__ == '__main__':
  41.     app = App()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-20 22:32:46 | 显示全部楼层    本楼为最佳答案   
本帖最后由 qq1151985918 于 2022-4-20 22:34 编辑

别的暂停可能还麻烦些,tkinter简单,只要弹出一个对话框阻塞住进程就好了
简单敲了一个样例

  1. from tkinter import Tk, StringVar, Label, Button
  2. from tkinter.messagebox import askokcancel
  3. from threading import Thread
  4. from time import sleep


  5. class App(Tk):
  6.     def __init__(self):
  7.         super().__init__()
  8.         self.geometry('100x120')
  9.         self.resizable(0, 0)
  10.         self.switch = ''
  11.         self.set_label = StringVar()
  12.         Label(self, textvariable=self.set_label).pack()
  13.         Button(self, text='开始', command=self.run).pack()
  14.         Button(self, text='暂停', command=lambda: self.set_switch('pause')).pack()
  15.         Button(self, text='停止', command=lambda: self.set_switch('stop')).pack()
  16.         self.mainloop()

  17.     def run(self):
  18.         def _run():
  19.             x = 0
  20.             while True:
  21.                 if self.switch == 'run':
  22.                     sleep(0.2)
  23.                     x += 1
  24.                     self.set_label.set(str(x))
  25.                     print(x)
  26.                 elif self.switch == 'pause':
  27.                     choose = askokcancel('提示:', '已暂停,点击确定继续,点击取消停止!')
  28.                     if choose:
  29.                         self.switch = 'run'
  30.                     else:
  31.                         self.switch = 'stop'
  32.                 else:
  33.                     return
  34.         self.switch = 'run'
  35.         t = Thread(target=_run)
  36.         t.setDaemon(True)
  37.         t.start()

  38.     def set_switch(self, x):
  39.         self.switch = x


  40. if __name__ == '__main__':
  41.     app = App()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-21 08:50:33 | 显示全部楼层
qq1151985918 发表于 2022-4-20 22:32
别的暂停可能还麻烦些,tkinter简单,只要弹出一个对话框阻塞住进程就好了
简单敲了一个样例

非常感谢 就是要这个效果
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-21 17:34:49 | 显示全部楼层
本帖最后由 cancry 于 2022-4-21 17:35 编辑
qq1151985918 发表于 2022-4-20 22:32
别的暂停可能还麻烦些,tkinter简单,只要弹出一个对话框阻塞住进程就好了
简单敲了一个样例


弱弱再问一句   我另外有一段自动操作excel表格 遍历行的函数 怎么把功能嵌套进去,能达到暂停 继续的效果?或者不需要按钮   只是通过键盘 空格键就能控制暂停和继续?谢谢!
def startpz():
        wb=vb.load_workbook('2月序时账(2).xlsx')
        # 打开活动工作表
        ws=wb.active
        for row in list(ws.rows)[1:]:
        # 按行 把每列的值 赋值到L数组
                l=[c.value for c in row]
                while True:
                        if l[0] is not None:
                                if l[1] == '财务':
                                        pyperclip.copy(l[0])
                                        pyautogui.press('backspace',interval=0.35)
                                        pyautogui.hotkey('ctrl','v',interval=0.45)
                                        pyautogui.press('enter',interval=0.25)
                                else:
                                        break
                        else:
                                break
                               
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 06:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表