我正在为自己的目的创建一个简单的时间计数程序。我在刷新数据方面遇到了麻烦,因为我不知道如何获取某些变量-它们超出了范围
当我试着运行程序时,它说:
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
如果要从calculate()函数返回所有这些值。您可以这样做:
然后调用函数
我就是这样做的,对我来说效果很好:
最终解决方案:
相关问题 更多 >
编程相关推荐