Python Forum
[Tkinter] cannot print DateEntry date
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] cannot print DateEntry date
#1
Can someone help me with DateEntry, printing a value? Now I'm stuck on this part and for some reason .get_date() does not work :/
import tkinter as tk
from tkinter import *
from tkcalendar import DateEntry

def Submit_Info():
    window1 = tk.Tk()
    window1.title("GUI EXAMPLE")              
    window1.geometry("300x300") 
    tk.Label(window1, text= "Country: " + variable.get(), font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=0, stick="w")  
    tk.Label(window1, text= "Area: " + variable1.get(), font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=1, stick="w")
    tk.Label(window1, text= "Team: " + variable2.get(), font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=2, stick="w")
    
    # does not give a date
    tk.Label(window1, text= "Start Date: " + st_date.get_date(), font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=3, stick="w")
    # does not give a date
    tk.Label(window1, text= "End Date: " + end_date.get_date(), font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=4, stick="w")
    
    tk.Label(window1, text= "Count per user: " + variable3.get(), font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=5, stick="w")
    window1.attributes('-topmost', True)       
    window1.mainloop() 

# GUI size, font, text size, title
Font_type = "Verdana"
Font_size = 10
window = tk.Tk()
text = tk.Text(window)
window.title("ANS Selector") # sets Title of Gui  
window.geometry("400x300") # sets size of GUI. Widht/Height

variable3 = tk.StringVar()

Countries = ["", "Denmark", "Finland", "Norway", "Sweden"]
variable = StringVar(window)
variable.set(Countries[0]) # default value
tk.Label(window, text= "Select Country:", font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=0, stick="w")
tk.OptionMenu(window, variable, *Countries).grid(column=1, row=0)

Areas = ["", "Business DK", "Business FI", "Business NO", "Business SE"]
variable1 = StringVar(window)
variable1.set(Areas[0]) # default value
tk.Label(window, text= "Select Area:", font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=1, stick="w")
tk.OptionMenu(window, variable1, *Areas).grid(column=1, row=1)

Team = ["AMLU"]
variable2 = StringVar(window)
variable2.set(Team[0]) # default value
tk.Label(window, text= "Select Team:", font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=2, stick="w")
tk.OptionMenu(window, variable2, *Team).grid(column=1, row=2)

tk.Label(window, text= "Start Date:", font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=3, stick="w")
st_date = DateEntry(window, values="Text", year=2021, state="readonly", date_pattern="yyyy-mm-dd").grid(column=1, row=3, padx=20, pady=5, sticky=W)

tk.Label(window, text= "End Date:", font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=4, stick="w")
end_date = DateEntry(window, values="Text", year=2021, state="readonly", date_pattern="yyyy-mm-dd").grid(column=1, row=4, padx=20, pady=5, sticky=W)

tk.Label(window, text= "Case per Employee:", font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=5, stick="w")
tk.Entry(window, textvariable = variable3, width=5).grid(column=1, row=5, stick="w")

tk.Button(window, text='SUBMIT', command=Submit_Info).grid(column=0, row=6, stick="w")  
window.attributes('-topmost', True)
window.mainloop() 
Reply
#2
st_date = DateEntry(window, values="Text", year=2021, state="readonly", date_pattern="yyyy-mm-dd").grid(column=1, row=3, padx=20, pady=5, sticky=W)
You are calling the method .grid on the DateEntry which means you loose the reference to the widget.
.grid will return None
split the creation of the widget and the call to grid into two lines.
st_date = DateEntry(window, values="Text", year=2021, state="readonly", date_pattern="yyyy-mm-dd")
st_date.grid(column=1, row=3, padx=20, pady=5, sticky=W)
Reply
#3
thank you, Yoriz. No I see that .get_date() was highlighted as a function, but it still does not give me an output. Maybe the syntax is wrong for the output?
import tkinter as tk
from tkinter import *
from tkcalendar import DateEntry

def Submit_Info():
    window1 = tk.Tk()
    window1.title("GUI EXAMPLE")              
    window1.geometry("300x300") 
    tk.Label(window1, text= "Country: " + variable.get(), font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=0, stick="w")  
    tk.Label(window1, text= "Area: " + variable1.get(), font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=1, stick="w")
    tk.Label(window1, text= "Team: " + variable2.get(), font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=2, stick="w")
     
    # does not give a date
    tk.Label(window1, text= "Start Date: " + st_date.get_date(), font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=3, stick="w")
    # does not give a date
    tk.Label(window1, text= "End Date: " + end_date.get_date(), font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=4, stick="w")
     
    tk.Label(window1, text= "Count per user: " + variable3.get(), font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=5, stick="w")
    window1.attributes('-topmost', True)       
    window1.mainloop() 
 
# GUI size, font, text size, title
Font_type = "Verdana"
Font_size = 10
window = tk.Tk()
text = tk.Text(window)
window.title("ANS Selector") # sets Title of Gui  
window.geometry("400x300") # sets size of GUI. Widht/Height
 
variable3 = tk.StringVar()
 
Countries = ["", "Denmark", "Finland", "Norway", "Sweden"]
variable = StringVar(window)
variable.set(Countries[0]) # default value
tk.Label(window, text= "Select Country:", font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=0, stick="w")
tk.OptionMenu(window, variable, *Countries).grid(column=1, row=0)
 
Areas = ["", "Business DK", "Business FI", "Business NO", "Business SE"]
variable1 = StringVar(window)
variable1.set(Areas[0]) # default value
tk.Label(window, text= "Select Area:", font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=1, stick="w")
tk.OptionMenu(window, variable1, *Areas).grid(column=1, row=1)
 
Team = ["AMLU"]
variable2 = StringVar(window)
variable2.set(Team[0]) # default value
tk.Label(window, text= "Select Team:", font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=2, stick="w")
tk.OptionMenu(window, variable2, *Team).grid(column=1, row=2)
 
tk.Label(window, text= "Start Date:", font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=3, stick="w")
st_date = DateEntry(window, values="Text", year=2021, state="readonly", date_pattern="yyyy-mm-dd")
st_date.grid(column=1, row=3, padx=20, pady=5, sticky=W)

tk.Label(window, text= "End Date:", font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=4, stick="w")
end_date = DateEntry(window, values="Text", year=2021, state="readonly", date_pattern="yyyy-mm-dd")
end_date.grid(column=1, row=4, padx=20, pady=5, sticky=W)

tk.Label(window, text= "Case per Employee:", font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=5, stick="w")
tk.Entry(window, textvariable = variable3, width=5).grid(column=1, row=5, stick="w")
 
tk.Button(window, text='SUBMIT', command=Submit_Info).grid(column=0, row=6, stick="w")  
window.attributes('-topmost', True)
window.mainloop()
Reply
#4
I installed tkcalendar to see what happens when the code is run and got the following error
Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Python39\lib\tkinter\__init__.py", line 1892, in __call__ return self.func(*args) File "***************************", line 14, in Submit_Info tk.Label(window1, text= "Start Date: " + st_date.get_date(), font=("{}".format(Font_type), "{}".format(Font_size))).grid(column=0, row=3, stick="w") TypeError: can only concatenate str (not "datetime.date") to str
You should include the error traceback when you make a post, it details what went wrong.

"Start Date: " + st_date.get_date()
"Start Date: " is a str,
st_date.get_date() returns a datetime.date they cannot be concatenated

You can use
https://docs.python.org/3/library/dateti...e.strftime Wrote:datetime.strftime(format)
Return a string representing the date and time, controlled by an explicit format string. For a complete list of formatting directives, see strftime() and strptime() Behavior.

You don't need to format a str into a str, its already a str
Font_type = "Verdana"
"{}".format(Font_type)

Your GUI only need one instance of tk.Tk() and .mainloop()

GUI code is easier to manage by using classes

flooding the namespace with
from tkinter import *
Reply
#5
Thank you, Yoriz! looks like now it works as I want. .place() is a bit annoying, when you need to set every element by defined x,y axis. In Autohotkey I found it more user-friendly, when you can just put x+10 y+15 and it will place that element regarding the element above.
import tkinter as tk
from tkinter import *
from tkcalendar import DateEntry

def Submit_Info():
    window1 = tk.Tk()
    window1.title("GUI EXAMPLE")              
    window1.geometry("300x300")
    window1['background']='#adc2eb'
    tk.Label(window1, text= "Country: " + variable.get(), font=(Font_type, Font_size)).grid(column=0, row=0, stick="w")  
    tk.Label(window1, text= "Area: " + variable1.get(), font=(Font_type, Font_size)).grid(column=0, row=1, stick="w")
    tk.Label(window1, text= "Team: " + variable2.get(), font=(Font_type, Font_size)).grid(column=0, row=2, stick="w")

    new_d = str(st_date.get_date()) # need to convert to string to place in GUI
    end_d = str(end_date.get_date())
    tk.Label(window1, text= "Start Date: " + new_d, font=(Font_type, Font_size)).grid(column=0, row=3, stick="w")
    tk.Label(window1, text= "End Date: " + end_d, font=(Font_type, Font_size)).grid(column=0, row=4, stick="w")

    tk.Label(window1, text= "Count per user: " + variable3.get(), font=(Font_type, Font_size)).grid(column=0, row=5, stick="w")
    window1.attributes('-topmost', True)       
    window1.mainloop() 
 
# GUI size, font, text size, title
Font_type = "Verdana"
Font_size = 10
el_w = 150 # elements width
sec_el_w = 180 # second row x axis
window = tk.Tk()
text = tk.Text(window)
window.title("ANS Selector") # sets Title of Gui  
window.geometry("350x300") # sets size of GUI. Widht/Height

#set window color
main_bg = "#adc2eb"
window['background']= main_bg

Countries = ["", "Denmark", "Finland", "Norway", "Sweden"]
variable = StringVar(window)
variable.set(Countries[0]) # default value
# Label is for Simple text in GUI
Font_size = 10
tk.Label(window, text= "Select Country:", bg= main_bg, font=(Font_type, Font_size)).place(x=5, y=5, width=el_w, height=25)
# OptionMenu is for DropDown  in GUI
tk.OptionMenu(window, variable, *Countries).place(x=sec_el_w, y=5, width=el_w, height=25)

Areas = ["", "Business DK", "Business FI", "Business NO", "Business SE"]
variable1 = StringVar(window)
variable1.set(Areas[0]) # default value
tk.Label(window, text= "Select Area:", bg= main_bg, font=(Font_type, Font_size)).place(x=5, y=35, width=el_w, height=25)
tk.OptionMenu(window, variable1, *Areas).place(x=sec_el_w, y=35, width=el_w, height=25)
 
Team = ["AMLU"]
variable2 = StringVar(window)
variable2.set(Team[0]) # default value
tk.Label(window, text= "Select Team:", bg= main_bg, font=(Font_type, Font_size)).place(x=5, y=65, width=el_w, height=25)
tk.OptionMenu(window, variable2, *Team).place(x=sec_el_w, y=65, width=el_w, height=25)
 
tk.Label(window, text= "Start Date:", bg= main_bg, font=(Font_type, Font_size)).place(x=5, y=95, width=el_w, height=25)
# DateEntry is for Calendar line in GUI
st_date = DateEntry(window, values="Text", year=2021, state="readonly", date_pattern="yyyy-mm-dd")
st_date.place(x=sec_el_w, y=95, width=el_w, height=25)

tk.Label(window, text= "End Date:", bg= main_bg, font=(Font_type, Font_size)).place(x=5, y=125, width=el_w, height=25)
end_date = DateEntry(window, values="Text", year=2021, state="readonly", date_pattern="yyyy-mm-dd")
end_date.place(x=sec_el_w, y=125, width=el_w, height=25)

tk.Label(window, text= "Case count per user:", bg= main_bg, font=(Font_type, Font_size)).place(x=5, y=155, width=el_w, height=25)
# Entry is for Input field in GUI
variable3 = tk.StringVar()
tk.Entry(window, textvariable = variable3, width=5).place(x=sec_el_w, y=155, width=el_w, height=25)

# Button is for Button in GUI
tk.Button(window, text='SUBMIT', command=Submit_Info).place(x=5, y=205, width=el_w, height=25) 
window.attributes('-topmost', True)
window.mainloop()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020