'dateEntry to only extract the year from user input instead of daymonthyear? In Python
I am creating a GUI using tkinter in python and I want to print the date of what the user had selected using dateEntry.
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.
Solution 1:[1]
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 )
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tls Chris |