如何暂停一个函数?
資深大佬 : yellowtail 6
代码在 jupyternotebook 里写的,奇怪的是在办公室能用,家里不能用了… 整个功能就是用 apscheduler 定时运行生成随机数然后在 tkinter 里可视化 现在想保留原代码里 root.after 功能(因为直接用 root.after,按现在做法定时函数就没用了,因为 root.after 是循环的) 感觉可以用锁的方法,但是水平捉急写不出来,求指教。。
”’ import datetime from apscheduler.schedulers.background import BackgroundScheduler import random import numpy as np from tkinter import * from tkinter import messagebox import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg import multiprocessing from threading import Thread import time
root = Tk() root.title("tkinter + Matplotlib") root.geometry('700x750') plt.rcParams['axes.unicode_minus']=False plt.rcParams['font.sans-serif'] = ['SimHei'] fig = plt.figure() ax1 = fig.add_subplot(1, 1, 1) ax2 = ax1.twinx() ax1.set_title('随机生成数') ax1.set_ylim() ax2.set_ylim() ax1.set_xlim(0, 200) ax2.set_xlim(0, 200) ax1.grid(True) ax1.set_xlabel("time") ax1.set_ylabel("随机数 1") ax2.set_ylabel("随机数 2") canvas = FigureCanvasTkAgg(fig, master=root) canvas.draw() canvas.get_tk_widget().pack() x = 0 ran1 = [] ran2 = [] times = [] def drawImg(): global ran1 global ran2 global times global x global ax1 global ax2 v1 = float(random.randint(-5, 5)) r1 = float(random.randint(-10, 10)) ran1.append(v1) ran2.append(r1) times.append(x) x += 1 ax1.clear() ax2.clear() ax1.axes.set_ylim(min(ran1)-5, 5+max(ran1)) ax2.axes.set_ylim(min(ran2)-5, 5+max(ran2)) ax1.axes.set_xlim(x-200+200, x+20) ax2.axes.set_xlim(x-200+20, x+20) ax1.plot(times, ran1,'y') ax2.plot(times, ran2,'r') canvas.draw() #global afterHandler #afterHandler = root.after(10, drawImg) #print(datetime.datetime.now()) def sub_task(): drawImg() def task_(time): def task__(): temp = BackgroundScheduler() temp.add_job(sub_task,'interval',seconds = 1,end_date =str(datetime.datetime.now())[:17]+time) temp.start() return task__ sche_fn = BackgroundScheduler() sche_fn.add_job(task_('25'),'cron',day_of_week = 'mon-fri',hour = '*',minute = '*',second = '0' ) sche_fn.start() sche_an = BackgroundScheduler() sche_an.add_job(task_('55'),'cron',day_of_week = 'mon-fri',hour = '*',minute = '*',second = '30' ) sche_an.start() def on_closing(): sche_an.shutdown() sche_fn.shutdown() #root.after_cancel(afterHandler) answer = messagebox.askokcancel("退出", "确定退出吗?") if answer: plt.close('all') root.destroy() else: root.after(1000, drawImg) root.protocol("WM_DELETE_WINDOW", on_closing) root.mainloop()
大佬有話說 (1)