[英]Function needs to be made before calling it, but variable has to be assigned before referenced
the get age function needs y获取年龄 function 需要 y
import math
import os
from tkinter import *
root = Tk()
root.title("DOG AGE CALCULATOR!!")
#define function to convert x.0 to x
def formatNumber(num):
if num % 1 == 0:
return int(num)
else:
return num
#print menu
def getAge():
# Convert y
if(y == 1):
dy = int(15)
elif(y == 2):
dy = int(24)
elif(y > 2):
dy = int(((y-2)*5)+24)
else:
p = 2
#convert m
if(y == 0):
dm = float(m*1.25)
elif(y == 1):
dm = float(m*0.75)
elif(y > 1):
dm = float(m*(5/12))
else:
p = 2
#add excess months to years
if(dm > 12):
dm = dm//12
y = y+(dm%12)
#add dog years and months
dogym = float(dy+dm)
#seperate dog month and year
dogmonth, dogyears = math.modf(dogym)
dogmonths = dogmonth*12
#formatting dog year and month
dogmonths = formatNumber(dogmonths)
dogyears = formatNumber(dogyears)
dogyearstr = str("{} Dog Years".format(dogyears))
dogmonthstr = str("{} Dog Months".format(dogmonths))
for widget in frame.winfo_children():
widget.destroy()
ylabel = Label(root, text=dogyearstr)
mlabel = Label(root, text=dogmonthstr)
ylabel.pack()
mlabel.pack()
title = Label(root, text="\/\/\!!DOG AGE CALCULATOR!!\/\/\\")
yentry = Entry(root)
mentry = Entry(root)
getagebutton = Button(root, text="Get Dog Age", command=getAge)
yentry.insert(0, "Dog's Age In Years")
mentry.insert(0, "Dog's Age In Months")
title.pack()
yentry.pack()
mentry.pack()
getagebutton.pack()
y = yentry.get()
m = mentry.get()
In this program, how do i fix the error:在这个程序中,我如何修复错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\name\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Users\name\PycharmProjects\DAC\maintmp.py", line 19, in getAge
if(y == 1):
^
UnboundLocalError: cannot access local variable 'y' where it is not associated with a value
(the file is maintmp.py) (文件为maintmp.py)
You should move the call to y = yentry.get()
inside the getAge()
function您应该在getAge()
function 内将调用移至y = yentry.get()
def getAge():
y = yentry.get()
# Convert y
if(y == 1):
dy = int(15)
elif(y == 2):
dy = int(24)
elif(y > 2):
dy = int(((y-2)*5)+24)
else:
p = 2
... # code omitted for brevity
You'll want to do a similar thing when get
ting the value of m
.在get
m
的值时,您会想要做类似的事情。 This way, the values are fetched from the entries when they're needed and can be updated accordingly.这样,在需要时可以从条目中获取值,并可以相应地进行更新。
As it's currently written, you're calling get()
on your Entry
widgets immediately after declaring them, so y
and m
will both be equal to None
正如目前所写的那样,您在声明它们后立即在您的Entry
小部件上调用get()
,因此y
和m
都等于None
You're getting the error UnboundLocalError: cannot access local variable 'y' where it is not associated with a value
because you're trying to perform a comparison operation on y
before it has any value (as far as getAge
is concerned)您收到错误UnboundLocalError: cannot access local variable 'y' where it is not associated with a value
因为您试图在y
具有任何值之前对其执行比较操作(就getAge
而言)
Also, unrelated to the error: the explicit conversions like int(15)
are unnecessary.此外,与错误无关:不需要像int(15)
这样的显式转换。 15 is already an int
eger. 15 已经是一个int
。 You can just do dy = 15
, dy = 24
, etc. Since y
is a string, you can just do the conversion on y
with y = int(yentry.get())
(the same goes for m
).您可以只执行dy = 15
、 dy = 24
等。由于y
是一个字符串,您可以使用y = int(yentry.get())
对y
进行转换(同样适用于m
)。
Then you don't need to worry about things like float(m*1.25)
either, since any number multiplied by a float
will return a float
!那么你也不需要担心像float(m*1.25)
这样的事情,因为任何数字乘以一个float
都会返回一个float
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.