从带有“选择日期”按钮的主窗口中获取顶级压光机的输出标签,tkinter

问题描述

我对如何获取主窗口标签中的顶级值有疑问。我的意思是我无法将输入的值从顶级窗口返回给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',y=150)
label1 = tk.Label(root,text="<-Click to select Test End Date",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,year=2020)         
    cal2.pack(fill="both",expand=True)
    ttk.Button(top2,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',command=calendar_view2)
button.place(x=500,y=150)
label2 = tk.Label(root,text="<-Click to select Test End Date",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,cursor="hand1",year=2020,date_pattern='y-mm-dd')         
    cal.pack(fill="both",expand=True)
    ttk.Button(top,command=print_sel).pack()
    # make window a modal window
    top.grab_set()
    top.wait_window(top)

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

button = tk.Button(root,command=lambda: calendar_view(startdate))

button = tk.Button(root,command=lambda: calendar_view(enddate))

并将两个变量与两个标签相关联,以便每当变量更新时它们的文本就会更新:

startdate = tk.StringVar(value="<-Click to select Test Start Date")
label1 = tk.Label(root,textvariable=startdate,8))

enddate = tk.StringVar(value="<-Click to select Test End Date")
label2 = tk.Label(root,textvariable=enddate,8))

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...