简体   繁体   English

使用 tkinter 为 Python 中的代码添加开始和停止按钮

[英]Add start and Stop Button for code in Python Using tkinter

I want to add Start and Stop Button for my code using tkinter我想使用tkinter为我的代码添加开始和停止按钮
I just want that when I click on start Button for scheduler.start() the code will run我只希望当我单击scheduler.start()启动按钮时,代码将运行
and when I click on stop button for scheduler.shutdown() code will stop当我单击scheduler.shutdown()停止按钮时,代码将停止
It is a Python 2.7 version but don't worry I just need the idea for this code that how it'll run它是Python 2.7版本,但别担心,我只需要知道如何运行此代码的想法
here is my code:这是我的代码:


if __name__ == '__main__':

    logging.basicConfig(filename='read.log', level=logging.INFO)
    logging.getLogger('apscheduler').setLevel(logging.DEBUG)
    scheduler = BackgroundScheduler()

    scheduler.add_job(calstk, 'interval', seconds=20)
  #  scheduler.add_job(calmrp, 'interval', seconds=10)
   # scheduler.add_job(caldisc, 'interval', seconds=15)



    # leave space only for understanding for which I need to add **start Button**

    scheduler.start()  # for this scheduler.start()



    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

    try:
       # stock = calstk()
        #print stock
        print '************************************************************************************'
        # This is here to simulate application activity (which keeps the main thread alive).
        while True:
            time.sleep(10)
    except (KeyboardInterrupt, SystemExit):
        # Not strictly necessary if daemonic mode is enabled but should be done if possible
        pass



    # leave space only for understanding for which I need to add **Stop Button**
scheduler.shutdown()
# for this scheduler.shutdown()


I want to add a start button for scheduler.start()我想为scheduler.start()添加一个开始按钮
and stop Button for scheduler.shutdown() .并停止scheduler.shutdown()的按钮。
I really need this code I almost build my project and I only need these two Button to handle my project.我真的需要这段代码,我几乎构建了我的项目,我只需要这两个按钮来处理我的项目。

import tkinter as tk
win = tk.Tk()
top = tk.Frame(win, padx = 20, pady = 20)
b1 = tk.Button(top, text='Start', padx = 10, pady = 10, command=scheduler.start)
b2 = tk.Button(top, text='Stop', padx = 10, pady = 10, command=scheduler.shutdown)
b1.pack(side=tk.LEFT)
b2.pack(side=tk.RIGHT)
top.pack()
win.mainloop()

This is how you can add buttons.这就是添加按钮的方法。

cant explain exactly how to do it, but here is video which probably could be helpful enter link description here无法准确解释如何做到这一点,但这里有可能有帮助的视频在此处输入链接描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM