在简单的计时程序中刷新统计数据

时间:2017-03-28 17:00:16

标签: python

我正在为自我目的创建一个简单的计时程序。由于我不知道如何获得某些变量,因此我无法获得令人耳目一新的统计数据 - 它们超出了范围。

当我尝试运行该程序时,它说:

  

追踪(最近一次通话):    文件" / home / cali / PycharmProjects / str8 / str8",第67行,in      显示()    文件" / home / cali / PycharmProjects / str8 / str8",第32行,显示      + str(round(years,2)),   NameError:name' years'未定义

以下是我所做的事情,一切似乎都适合我,除了变量年,周,日之下的红色波浪线......

# str8.py
#   Program to count time from a certain event

from tkinter import *
from datetime import *

root = Tk()
root.title("STR8")
root.resizable(width=False, height=False)

def calculate():
    event = datetime(2017, 3, 28, 16, 0, 0)
    tday = datetime.now()

    str8 = tday - event

    seconds = str8.total_seconds()
    minutes = str8.total_seconds() / 60
    hours = minutes / 60
    days = hours / 24
    weeks = days / 7
    years = weeks / 52

def display():

    thelabel = Label(root,
                     text = "You have been STR8 for:\n",
                     font = "Verdana 8 bold").grid(row=0, sticky=    W)

    labelYears = Label(root,
                       text = "Years: "
                       + str(round(years, 2)),
                       font = "Verdana 8").grid(row = 1, sticky=W)

    labelWeeks = Label(root,
                       text = "Weeks: "
                       + str(round(weeks, 2)),
                       font = "Verdana 8").grid(row=2, sticky=W)

    labelDays = Label(root,
                      text = "Days: "
                      + str(round(days, 2)),
                      font = "Verdana 8").grid(row=3, sticky=W)

    labelHours = Label(root,
                       text = "Hours: "
                       + str(round(hours, 2)),
                       font = "Verdana 8").grid(row=4, sticky=W)

    labelMinutes = Label(root,
                         text = "Minutes: "
                         + str(round(minutes)),
                         font = "Verdana 8").grid(row=5, sticky=W)

    labelSeconds = Label(root,
                         text = "Seconds: "
                         + str(round(str8.total_seconds(), 2)),
                         font = "Verdana 8").grid(row=6, sticky=W)

    buttonRefresh = Button(root,
                           text =  "Refresh",
                           font = "Verdana 8",
                           height = 1,
                           width = 19,
                           command = refresh).grid(row=7)
calculate()
display()

def refresh():
    calculate()
    display()

root.mainloop()

我正在使用Python 3.6。

3 个答案:

答案 0 :(得分:0)

如果要从calculate()函数返回所有这些值。你可以这样做:

from datetime import *
def calculate():
    event = datetime(2017, 3, 28, 16, 0, 0)

    tday = datetime.now()

    str8 = tday - event

    seconds = str8.total_seconds()

    minutes = str8.total_seconds() / 60

    hours = minutes / 60

    days = hours / 24

    weeks = days / 7

    years = weeks / 52

    return event, tday, str8, seconds, minutes, hours, days, weeks, years

然后将函数调用为

e, t, s8, s, m, h, d, w, y = calculate()

答案 1 :(得分:0)

最终解决方案:

# str8.py
#   Program to count time from a certain event
from tkinter import *
from datetime import *

root = Tk()
root.title("STR8")
root.resizable(width=False, height=False)

def calculate():
    event = datetime(2017, 3, 28, 16, 0, 0)
    tday = datetime.now()

    str8 = tday - event

    seconds = str8.total_seconds()
    minutes = str8.total_seconds() / 60
    hours = minutes / 60
    days = hours / 24
    weeks = days / 7
    years = weeks / 52

    return event, tday, str8, seconds, minutes, hours, days, weeks, years

def refresh():
    calculate()
    display()

def display():
    event, tday, str8, seconds, minutes, hours, days, weeks, years = calculate()

    thelabel = Label(root,
                     text = "You have been STR8 for:\n",
                     font = "Verdana 8 bold").grid(row=0, sticky=W)

    labelYears = Label(root,
                       text = "Years: "
                       + str(round(years, 2)),
                       font = "Verdana 8").grid(row = 1, sticky=W)

    labelWeeks = Label(root,
                       text = "Weeks: "
                       + str(round(weeks, 2)),
                       font = "Verdana 8").grid(row=2, sticky=W)

    labelDays = Label(root,
                      text = "Days: "
                      + str(round(days, 2)),
                      font = "Verdana 8").grid(row=3, sticky=W)

    labelHours = Label(root,
                       text = "Hours: "
                       + str(round(hours, 2)),
                       font = "Verdana 8").grid(row=4, sticky=W)

    labelMinutes = Label(root,
                         text = "Minutes: "
                         + str(round(minutes)),
                         font = "Verdana 8").grid(row=5, sticky=W)

    labelSeconds = Label(root,
                         text = "Seconds: "
                         + str(round(str8.total_seconds(), 2)),
                         font = "Verdana 8").grid(row=6, sticky=W)

    buttonRefresh = Button(root,
                        text =  "Refresh",
                        font = "Verdana 8",
                        height = 1,
                        width = 19,
                        command = refresh).grid(row=7)
calculate()
display()



root.mainloop()

答案 2 :(得分:-2)

我是这样做的,对我来说效果很好:

def calculate():
    global event
    event = datetime(2017, 3, 28, 16, 0, 0)

    global tday
    tday = datetime.now()

    global str8
    str8 = tday - event

    global seconds
    seconds = str8.total_seconds()

    global minutes
    minutes = str8.total_seconds() / 60

    global hours
    hours = minutes / 60

    global days
    days = hours / 24

    global weeks
    weeks = days / 7

    global years
    years = weeks / 52