Search code examples
pythontkintercalendarwidgettoplevel

Get the output label of calender toplevel from the master window having "Select Date" button, tkinter


I have a doubt, how to get the top-level value in the master window's label. I mean like i couldn't return the entered value from toplevel window to main's self. I tried other ways and it didn't work

import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tkcalendar import Calendar, DateEntry
  
class Application(tk.Frame):
    
    def _init_(self,master):
        Frame._init_(self,master)
        self.master=master
        self.label=Label(text="",font=("Georgia,12"))
        self.label.place(x=50,y=80)
        self.create_widgets() 
    

def calendar_view():
    def print_sel():
        return cal.get()
        
    top = tk.Toplevel(root)
        
    cal = Calendar(top,
                   font="Arial 14",selectmode='day',
                    cursor="hand1", year=2020)         
    cal.pack(fill="both", expand=True)
    ttk.Button(top, text="ok", command=print_sel).pack()                   
root=tk.Tk()

s = ttk.Style(root)
s.theme_use('clam')
root.title("Date")
root.geometry("800x500")

button = tk.Button(root, text = 'Test Start Date', bg='#0073BD',height=1,width=15,padx=5,pady=2, fg="white",command=calendar_view)
button.place(x=500,y=100)
label1 = tk.Label(root, text="<-Click to select Test Start Date", padx=10, pady=10, bg="#e6e9f2", font=("calibri",8))
label1.place(x=630,y=100)

button = tk.Button(root, text = 'Test End Date', bg='#0073BD',height=1,width=15,padx=5,pady=2,fg="white",command=calendar_view)
button.place(x=500,y=150)
label1 = tk.Label(root, text="<-Click to select Test End Date", padx=10, pady=10, bg="#e6e9f2", font=("calibri",8))
label1.place(x=630,y=150)

app=Application(root)
root.configure(bg='#e6e9f2')
root.mainloop()

[here is the reference of the image]1


Solution

  • @acw1668 Here is the updated code, can you send me your version updated code

    import tkinter as tk
    from tkinter import ttk
    from tkinter import filedialog
    from tkcalendar import Calendar, DateEntry
      
    class Application(tk.Frame):
        
        def _init_(self,master):
            Frame._init_(self,master)
            self.master=master
            self.label=Label(text="",font=("Georgia,12"))
            self.label.place(x=50,y=80)
            self.create_widgets() 
        
    caldate1 = " "
    caldate2 = " "
        
    def calendar_view1():
        def print_sel1():
            global caldate1
            caldate1 = cal1.selection_get()
            label1.config(text = caldate1)
            return caldate1
        
        top1 = tk.Toplevel(root)
        cal1 = Calendar(top1,
                       font="Arial 14",selectmode='day',
                        cursor="hand2", year=2020)         
        cal1.pack(fill="both", expand=True)
        ttk.Button(top1, text="ok", command=print_sel1).pack()  
        
    def calendar_view2():
        def print_sel2():
            global caldate2
            caldate2 = cal2.selection_get()
            label2.config(text = caldate2)
            return caldate2
        
        top2 = tk.Toplevel(root)
        cal2 = Calendar(top2,
                       font="Arial 14",selectmode='day',
                        cursor="hand2", year=2020)         
        cal2.pack(fill="both", expand=True)
        ttk.Button(top2, text="ok", command=print_sel2).pack()
    
        
    root=tk.Tk()
    
    s = ttk.Style(root)
    s.theme_use('clam')
    root.title("Date")
    root.geometry("800x500")
    
    button = tk.Button(root, text = 'Test Start Date', bg='#0073BD',height=1,width=15,padx=5,pady=2, fg="white",command=calendar_view1)
    button.place(x=500,y=100)
    label1 = tk.Label(root, text="<-Click to select Test Start Date", padx=10, pady=10, bg="#e6e9f2", font=("calibri",8))
    label1.place(x=630,y=100)
    
    button = tk.Button(root, text = 'Test End Date', bg='#0073BD',height=1,width=15,padx=5,pady=2,fg="white",command=calendar_view2)
    button.place(x=500,y=150)
    label2 = tk.Label(root, text="<-Click to select Test End Date", padx=10, pady=10, bg="#e6e9f2", font=("calibri",8))
    label2.place(x=630,y=150)
    
    app=Application(root)
    root.configure(bg='#e6e9f2')
    root.mainloop()