我想get_date()
在 widget上使用方法DateEntry
。我只想让这个日期做到期剩余。但是 ir 引发了一个错误:TypeError: can't compare datetime.datetime to datetime.date
. 如何摆脱这个错误?
部分代码如下:
from tkinter import *
from tkinter import ttk
from tkcalendar import Calendar,DateEntry
from datetime import datetime,timedelta
root = Tk()
root.title("Medicine database")
curr_date = datetime.now()
time_diff = timedelta(days=30)
reqdate = curr_date + time_diff
def add_medicine():
tree.insert("",END,values=(e0.get(),e1.get(),e2.get(),cal1.get()))
e0.delete(0,END)
e1.delete(0,END)
e2.delete(0,END)
#here I used method get_date()
expiry_date = cal1.get_date()
if reqdate == expiry_date:
print("The product has expired")
#etykiety
lb0 = Label(root,text="Serial number")
lb0.grid(row=0,column=0,rowspan=1,columnspan=1)
lb1 = Label(root,text="Medicine name")
lb1.grid(row=0,column=0,rowspan=2,columnspan=1)
lb2 = Label(root,text="Quantity")
lb2.grid(row=0,column=0,rowspan=3,columnspan=1)
lb3 = Label(root,text="Expiry date")
lb3.grid(row=1,column=0,rowspan=2,columnspan=1)
#okienka
e0 = Entry(root,width=15)
e0.grid(row=0,column=1,rowspan=1,sticky=W)
e1 = Entry(root,width=15)
e1.grid(row=0,column=1,rowspan=2,sticky=W)
e2 = Entry(root,width=15)
e2.grid(row=0,column=1,rowspan=3,sticky=W)
cal1 = DateEntry(root,width=12,bg="darkblue",fg="white",date_pattern="dd/mm/yyyy",year=2020,state="readonly")
cal1.grid(row=1,column=1,rowspan=2,sticky=W)
#klawisze
btn1 = Button(root,text="Add new\nmedicine",width=10,command=add_medicine)
btn1.grid(row=1,column=1,padx=15,rowspan=3)
#treeview
tree = ttk.Treeview(root,height=25)
tree["columns"]=("one","two","three","four")
tree.column("one",width=120)
tree.column("two",width=160)
tree.column("three",width=130)
tree.column("four",width=160)
tree.heading("one", text="Serial number")
tree.heading("two", text="Medicine name")
tree.heading("three", text="Quantity")
tree.heading("four",text="Expiry date")
tree["show"]="headings"
tree.grid(row=0,column=2,rowspan=6,pady=20)
scbr2= Scrollbar(root,orient="vertical",command=tree.yview)
scbr2.grid(row=0,column=3,sticky=W,columnspan=1,rowspan=6,padx=1,ipady=240)
tree.config(yscrollcommand=scbr2.set)
scbr3= Scrollbar(root,orient="horizontal",command=tree.xview)
scbr3.grid(row=5,column=2,sticky=S,ipadx=260)
tree.config(xscrollcommand=scbr3.set)
root.geometry("840x580")
root.mainloop()