Python在5分钟后重新运行脚本

时间:2018-04-03 18:19:55

标签: python tkinter

我有一个 tkinter GUI表,用于从文件加载数据。 我试着做以下代码但没有成功:

import schedule
import time
def job():
  root = tkinter.Tk()
  root.title("APP")
  root.configure(background='khaki')
  root.wm_attributes("-topmost", 1)
  for r in range(11):
  for c in range(8):
   if c==0 and r==0:
    tkinter.Label(root, text="AAA",borderwidth=1,font=("Helvetica", 13)).grid(row=r, column=c)
  root.mainloop()

schedule.every(5).minutes.do(job)

while True:
 schedule.run_pending()
 time.sleep(1)

它运行但是5分钟之后没有变化。

我需要帮助才能每5分钟重新运行一次脚本,将数据重新加载到表格中。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

您需要添加一个以after(...)回调结束的刷新:

class my_frame(tk.Frame):

    def refresh(self):
        # do table update
        self.after(5*60*1000, self.refresh)  # 5 minutes in milliseconds

这会导致root.mainloop()定期刷新自己。

没有自定义框架:

def refresh():
    # do stuff
    root.after(5*60*1000, refresh)  # 5 minutes in milliseconds

root.after(5*60*1000, refresh)

答案 1 :(得分:0)

tkinter具有以下功能:

@staticmethod def hi(): print "hi"

所以你可以使用:

widget.after(milliseconds, function, *args)