在一个简单的计时程序中刷新统计信息

2024-06-28 18:44:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为自己的目的创建一个简单的时间计数程序。我在刷新数据方面遇到了麻烦,因为我不知道如何获取某些变量-它们超出了范围

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

Traceback (most recent call last): File "/home/cali/PycharmProjects/str8/str8", line 67, in display() File "/home/cali/PycharmProjects/str8/str8", line 32, in display + str(round(years, 2)), NameError: name 'years' is not defined

这是我所做的一切,似乎对我来说都是正确的,除了在变量年,周,日下的红色曲线

# 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()

我使用的是python3.6


Tags: textfromdisplayrootlabelgridrowseconds
3条回答

如果要从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()

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

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

最终解决方案:

# 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()

相关问题 更多 >