user13827334
user13827334

Reputation:

Don't understand cause of this TypeError with method get_time()

I'd like to use method get_date() on widget DateEntry. I just want to get this date to do expiry remainder. But ir raises an error: TypeError: can't compare datetime.datetime to datetime.date. How to get rid of this error?

Part of code below:

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

Upvotes: 0

Views: 96

Answers (1)

martineau
martineau

Reputation: 123473

The error is occurring because the get_date() method returns a datetime.date instance, however the reqdate value your code is calculating is a datetime.datetime.

You should be able to avoid the problem by making reqdate the same type that is being returned by the method with something along these lines:

from datetime import datetime, timedelta, date
...

#curr_date = datetime.now()
curr_date = date.today()
time_diff = timedelta(days=30)
reqdate = curr_date + time_diff
...

Upvotes: 1

Related Questions