我当时正在使用Tkinter制作“日期计算器” GUI,其中,如果一个人输入了今天的日期,并且想知道从该日期开始的某些天之后的日期,那么他可以通过此GUI知道,例如,如果我输入了Date = 2019-04-05和Skip = 2天,则它将在控制台中返回2019-04-07作为答案。但是出现错误,表明Year 0超出范围,请查看代码
import datetime
from tkinter import *
pydi=Tk()
pydi.geometry("720x120")
#Function
def Harsh():
Date=datetime.date(y,m,d)
Next=datetime.timedelta(f)
New=Date+Next
print(New)
#Label With Grid Packing (One liner)
Year=Label(pydi,text="Enter The Year").grid(row=1,column=1)
Month=Label(pydi,text="Enter The Month").grid(row=2,column=1)
Day=Label(pydi,text="Enter The Day").grid(row=3,column=1)
Skip=Label(pydi,text="Enter Skip Days").grid(row=4,column=1)
#Values
YearValue=IntVar()
MonthValue=IntVar()
DayValue=IntVar()
SkipValue=IntVar()
#Using Get to convert Tkinter IntVar to python int
y=YearValue.get()
m=MonthValue.get()
d=DayValue.get()
f=SkipValue.get()
#Entry Widget
YearEntry=Entry(pydi,textvariable=YearValue).grid(row=1,column=2)
MonthEntry=Entry(pydi,textvariable=MonthValue).grid(row=2,column=2)
DayEntry=Entry(pydi,textvariable=DayValue).grid(row=3,column=2)
SkipEntry=Entry(pydi,textvariable=SkipValue).grid(row=4,column=2)
#Button
Calculate=Button(pydi,bg="black",fg="white",text="Calculate",command=Harsh).grid(row=17,column=2)
pydi.mainloop()
答案 0 :(得分:0)
直接使用
def Harsh():
#Date=datetime.date(y,m,d)
Date = datetime.date(YearValue.get(), MonthValue.get(), DayValue.get())
Next=datetime.timedelta(f)
New=Date+Next
print(New)
反正问题在下面显示,
print(y,m,d)返回0,0,0
#Function
def Harsh():
print(y,m,d)
Date=datetime.date(y,m,d)
Next=datetime.timedelta(f)
New=Date+Next
print(New)