从具有 "Select Date" 按钮的 master window 获取日历顶层的输出标签,tkinter

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

我有一个疑问,如何在大师window的标签中获取顶级值。我的意思是我无法 return 从顶层 window 到 main 自身的输入值。 我尝试了其他方法,但没有用

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

@acw1668 这是更新后的代码,能把你的版本更新后的代码发给我吗

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()

建议使用两个StringVar来存储开始日期和结束日期,并将变量传递给calendar_view():

def calendar_view(var):
    def print_sel():
        # update the var
        var.set(cal.get_date())
        # close the calendar window
        top.destroy()
        
    top = tk.Toplevel(root)
    cal = Calendar(top, font="Arial 14",selectmode='day',
                    cursor="hand1", year=2020, date_pattern='y-mm-dd')         
    cal.pack(fill="both", expand=True)
    ttk.Button(top, text="ok", command=print_sel).pack()
    # make window a modal window
    top.grab_set()
    top.wait_window(top)

然后更新两个按钮的创建:

button = tk.Button(root, text = 'Test Start Date', bg='#0073BD',height=1,width=15,padx=5,pady=2, fg="white",
                   command=lambda: calendar_view(startdate))

button = tk.Button(root, text = 'Test End Date', bg='#0073BD',height=1,width=15,padx=5,pady=2,fg="white",
                   command=lambda: calendar_view(enddate))

并将这两个变量关联到两个标签,以便在变量更新时更新它们的文本:

startdate = tk.StringVar(value="<-Click to select Test Start Date")
label1 = tk.Label(root, textvariable=startdate, padx=10, pady=10, bg="#e6e9f2", font=("calibri",8))

enddate = tk.StringVar(value="<-Click to select Test End Date")
label2 = tk.Label(root, textvariable=enddate, padx=10, pady=10, bg="#e6e9f2", font=("calibri",8))