Python Forum
[Tkinter] Retrieving a value from a calendar with a single click
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Retrieving a value from a calendar with a single click
#1
Hello,
I want to recover the date selected in the calendar which opens with a single click and as soon as I confirm with the OK button I can insert it in the "entry_date" field.
i don't see how to do it, could you please help me.
thank you very much in advance.

but I have an error message on line 30, however I recover my function "result_selected()"

Quote: entry_date = Entry(root,result_select())
NameError: name 'result_select' is not defined


# coding:utf-8
from tkinter import *

try:
    import tkinter as tk
    from tkinter import ttk
except ImportError:
    import tkinter as tk
    import ttk

from tkcalendar import Calendar, DateEntry


def calendar1(event):
    def result_select():
        return cal.selection_get()


    top = tk.Toplevel(root)
    cal = Calendar(top, font="Arial 9", selectmode='day', locale='en_US',
                   cursor="hand1", year=2020, month=2, day=5)
    cal.pack(fill="both", expand=True)
    ttk.Button(top, text="ok", command=result_select).pack()

# Window
root = Tk()
root.geometry("500x500")

entry_date_label = Label(root, text="Entry date simple click").pack()
entry_date = Entry(root,result_select())
entry_date.pack()

# Active calendar
entry_date.bind('<Button-1>', calendar1)  # simple clic

root.mainloop()
Reply
#2
like this?:
# coding:utf-8
from tkinter import *

from tkcalendar import Calendar, DateEntry

def get_my_date():
    date= entry_date.get()
    print(date)

root = Tk()
root.geometry("500x500")
entry_date_label = Label(root, text="Entry date simple click").pack()
entry_date = DateEntry(root,)
entry_date.pack()
btn_submit= Button(root, text='submit',command= get_my_date)
btn_submit.pack()
root.mainloop()
Reply
#3
Hello Joe,
Thank you very much and it works well.

But here the selection field looks like a combobox.
Can you help me understand if I'm not overdoing it, how to do it for the 2nd method.
How to make so that by a double click in this case I can recover the date selected in the calendar?
Reply
#4
when you created the entry widget you added a function call to result_selected and you
get an error. Entries don't call function that's where the bind key name the function
used. so first of change your entry like so:
entry_date = Entry(root)
then when you click on the entry it will call the calendar function calendar1.
you can use print instead of return
def calendar1(event):
    def result_select():
        print(cal.selection_get())# ...rest of your function
do a search of tkcalendar they made a document pdf with examples and attributes etc.
Reply
#5
Thank you very much.
You are nice.
I look, I do the research and if I need I come back to you. Smile Smile Smile

Its OK !!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to set the calendar widget to current date scratchmyhead 4 7,031 May-11-2020, 08:27 PM
Last Post: scratchmyhead
  Tkinter calendar widget scratchmyhead 4 4,249 May-03-2020, 07:01 PM
Last Post: scratchmyhead

Forum Jump:

User Panel Messages

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