如何显示每小时的运行时间和循环

时间:2015-06-24 19:55:09

标签: python loops datetime

我有一个操作gpio的脚本然后循环,直到循环被tkinter按钮打破,id喜欢看它每小时做多少循环。我已经能够看到它使用全局变量完成了多少循环,但无法解决,也没有找出如何使它显示当前运行时间和每小时循环数量..请帮助.. 这是MCVE

import Tkinter as tk
root = tk.Tk()
from Tkinter import *
import time
import datetime


root.geometry("748x150")
root.title("Countdown Tester")

run_counter = 0

ts = datetime.datetime.now()
tf = datetime.datetime.now()
te = tf - ts


def _1():
    label2["text"] = "1"
    label2.config(bg='orange', fg='black')
    root.update_idletasks()
    time.sleep(1)


def _0():
    label2["text"] = "0"
    label2.config(bg='orange', fg='black')
    root.update_idletasks()
    time.sleep(1)
    global run_counter
    run_counter += 1
    root.update_idletasks()
    label6["text"] = run_counter
    label7["text"] = run_counter# *********** Id like to have the run times per hour listed here
    label8["text"] = te# *********** Id like to have the total run time listed here
    root.update_idletasks()


canvas = Canvas(width=720, height=150, bg='black')
canvas.grid(rowspan=26, columnspan=20, sticky='W,E,N,S')

label2 = Label(root, width=11, font='-weight bold', background='black', foreground='white')
label2.grid(padx=5, pady=10, row=0, column=0, sticky='W,E,N,S')
label6 = Label(root, text=run_counter, width=11, font='-weight bold', background='white', foreground='black')
label6.grid(padx=5, pady=10, row=0, column=1, sticky='W,E,N,S')
label7 = Label(root, text=run_counter, width=11, font='-weight bold', background='white', foreground='black')
label7.grid(padx=5, pady=10, row=1, column=1, sticky='W,E,N,S')
label8 = Label(root, text=te, width=11, font='-weight bold', background='white', foreground='black')
label8.grid(padx=5, pady=10, row=1, column=0, sticky='W,E,N,S')

#root.resizable(width=FALSE, height=FALSE)
#timer()

while 1:
    try:
        _1()
        _0()
        root.update()
    except KeyboardInterrupt:
        break


root.mainloop()

出于某种原因,这个MCVE在退出时报告了_1()的错误

0 个答案:

没有答案