Home > Net >  dateEntry to only extract the year from user input instead of daymonthyear? In Python
dateEntry to only extract the year from user input instead of daymonthyear? In Python

Time:04-05

I am creating a GUI using tkinter in python and I have managed to print the date of what the user had selected using dateEntry but I am wondering if it is possible to only extract the year of the user?

Here is what my code looks like:

import tkinter
from tkinter import *
from tkinter import ttk
from tkcalendar import DateEntry

main = Tk()
main.geometry("750x250")
main.title("Publication Date Search")

def getUserDate():
    date = ED.get()
    print(date)

L1 = Label(main, text="Publication Year", fg= 'black')
L1.grid(row=0,column=0,padx=5,pady=10)

ED = DateEntry(main)
ED.grid(row=0, column=2)
buttonsub = Button(main, text='Search', command=getUserDate)
buttonsub.grid(row=3, column=2)

main.mainloop()

I couldn't really find much content online in regards to this, it all included the day/month/year.

CodePudding user response:

The DateEntry object can return a date object, using .get_date() instead of .get(). Using .get returns a string.

def getUserDate(): 
    date = ED.get_date() 
    print(date, type(date), date.year )
    
  • Related