2

The doubt was the following, I want to select a date using the tkcalendar library, it is selected correctly, but I cannot use the variable outside the function.

def dateentry_view():
    def print_sel():
        date = cal.get_date()
        print(date)
    top = tk.Toplevel(root)

    ttk.Label(top, text='Elige el día').pack()
    cal = DateEntry(top)
    cal.pack(padx=10, pady=10)
    ttk.Button(top, text="Aceptar", command=print_sel).pack()

How can I pass the date variable to display it in a Label as follows:

labelDate = Label(root,textvariable=date)

I have tried to put the Label inside the function, but it still doesn't show the date variable.

def dateentry_view():
    
    top = tk.Toplevel(root)

    ttk.Label(top, text='Elige el día').pack()
    cal = DateEntry(top)
    cal.pack(padx=10, pady=10)
    ttk.Button(top, text="Aceptar", command=print_sel).pack() 

    def print_sel():
         date = cal.get_date()
         print(date)
         labelFecha = Label(root,textvariable=date)

When I print date it shows me the date I have selected correctly.

martineau
  • 119,623
  • 25
  • 170
  • 301
Darío
  • 23
  • 5

2 Answers2

0

From googling tk.Label it looks like the idea of textvariable is that it refers to a mutable tk.StringVar, not a normal Python str (which is immutable). Hence all you need to do is declare the StringVar in the outer scope and then update it inside the callback:

    date = tk.StringVar()
    def set_date():
         date.set(cal.get_date())

    ttk.Button(top, text="Aceptar", command=set_date).pack() 
    labelFecha = Label(root, textvariable=date)
Samwise
  • 68,105
  • 3
  • 30
  • 44
0

You're not using the textvariable- option properly. You need to set it a reference to an instance of a tk.StringVar variable class. Afterwards any changes to the variable's value will automatically update what the widget displays.

Also note that the tkcalendar.DateEntry.get_date() method returns a datetime.date, not a string, so you'll need manual to convert it into one yourself before setting the StringVar's value to it.

Here's a runnable example illustrating what I am saying:

import tkinter as tk
import tkinter.ttk as ttk
from tkcalendar import DateEntry


def dateentry_view():
    def print_sel():
        date = cal.get_date()  # Get datetime.date.
        fechaStr = date.strftime('%Y-%m-%d')  # Convert datetime.date to string.
        fechaVar.set(fechaStr)  # Update StringVar with formatted date.
        top.destroy()  # Done.

    top = tk.Toplevel(root)

    ttk.Label(top, text='Elige el día').pack()
    cal = DateEntry(top)
    cal.pack(padx=10, pady=10)
    ttk.Button(top, text="Aceptar", command=print_sel).pack()


root = tk.Tk()

ttk.Button(root, text="Ejecutar prueba", command=dateentry_view).pack()
fechaVar = tk.StringVar(value='<no date>')  # Create and initialize control variable.
labelFecha = tk.Label(root, textvariable=fechaVar)  # Use it.
labelFecha.pack()

root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301