Python Tkinter:在帧之间传递日历选择

Python Tkinter:在帧之间传递日历选择,python,tkinter,Python,Tkinter,我正在用tkinter设置一个GUI,并希望设置一个日期选择器,它可以将用户选择的日期传递到下一帧。我尝试过使用全局变量,但从未成功过。我得到的最接近的方法是使用控制器创建每个帧的页面实例,然后使用此对象访问其他帧属性。在“存款”框架中,我创建了“日期选择”框架的一个实例,以便访问Calendar对象并使用get_date()函数。但是,创建日历对象时,它始终返回默认日期,而不是用户选择的日期 我的代码包含一个用于访问API的初始登录框架。在我解决此问题时,应用程序会忽略此框架并直接转到Date

我正在用tkinter设置一个GUI,并希望设置一个日期选择器,它可以将用户选择的日期传递到下一帧。我尝试过使用全局变量,但从未成功过。我得到的最接近的方法是使用控制器创建每个帧的页面实例,然后使用此对象访问其他帧属性。在“存款”框架中,我创建了“日期选择”框架的一个实例,以便访问Calendar对象并使用get_date()函数。但是,创建日历对象时,它始终返回默认日期,而不是用户选择的日期

我的代码包含一个用于访问API的初始登录框架。在我解决此问题时,应用程序会忽略此框架并直接转到DateSelect框架,并且API端点url和凭据已被删除

我的代码:

import tkinter as tk
from tkinter import ttk
import tessitura
from tkcalendar import *
import datetime
from datetime import date

LARGEFONT = ("Verdana", 15)
endpoint=""

class tkinterApp(tk.Tk):

    # __init__ function for class tkinterApp
    def __init__(self, *args, **kwargs):
        # __init__ function for class Tk
        tk.Tk.__init__(self, *args, **kwargs)
        self.title("Box Office Deposit")
        self.geometry("1200x800")
        # creating a container
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        # initializing frames to an empty array
        self.frames = {}

        # iterating through a tuple consisting
        # of the different page layouts
        for F in (Login, DateSelect, Deposit):
            frame = F(container, self)

            # initializing frame of that object from
            # login, dateselect, deposit respectively with
            # for loop
            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(DateSelect)

    # to display the current frame passed as
    # parameter
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

    def get_page(self, page_class):
        return self.frames[page_class]


# first window frame Login
class Login(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        # label of frame Layout 2
        label = ttk.Label(self, text="RAMP Credentials", font=LARGEFONT)
        invalid = ttk.Label(self)
        # putting the grid in its place by using
        # grid
        label.grid(row=0, column=0, padx=10, pady=10)
        invalid.grid(row=4, column=0)
        ttk.Label(self, text="Username").grid(row=1, column=0)
        ttk.Label(self, text="Password").grid(row=2, column=0)
        e1 = ttk.Entry(self)
        e2 = ttk.Entry(self, show="*")
        e1.grid(row=1, column=1)
        e2.grid(row=2, column=1)

        def validate_login():
            invalid.config(text="")
            global username, password, credentials
            username = e1.get()
            password = e2.get()
            method = ""
            request_type = "GET"
            params = {}
            data = {}
            credentials = 
            login = tessitura.rest_call(endpoint, credentials, request_type, method, params=params, data=data,
                                        timeout=10)
            if "ActiveDirectoryUserName" in login:
                return controller.show_frame(DateSelect)
                e1.delete(0, 'end')
                e2.delete(0, 'end')
            else:
                return invalid.config(text="Invalid Login")
                e2.delete(0, 'end')

        button1 = ttk.Button(self, text="Login",
                             command=lambda: validate_login())

        # putting the button in its place by
        # using grid
        button1.grid(row=3, column=1, padx=10, pady=10)

        ## button to show frame 2 with text layout2
        #button2 = ttk.Button(self, text="Page 2",
         #                    command=lambda: controller.show_frame(Page2))

        # putting the button in its place by
        # using grid
        #button2.grid(row=2, column=1, padx=10, pady=10)


# second window frame page1
class DateSelect(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = ttk.Label(self, text="Please Select Deposit Date", font=LARGEFONT)
        label.grid(row=0, column=4, padx=10, pady=10)

        self.cal = Calendar(self, selectmode="day", year=2021, month=5, day=13)
        self.cal.grid(row=3, column=4, padx=10, pady=10)

        # button to show frame 2 with text
        # layout2
        button1 = ttk.Button(self, text="Logout",
                             command=lambda: controller.show_frame(Login))

        # putting the button in its place
        # by using grid
        button1.grid(row=1, column=1, padx=10, pady=10)

        # button to show frame 2 with text
        # layout2
        button2 = ttk.Button(self, text="Get Deposit",
                             command=lambda: controller.show_frame(Deposit))

        # putting the button in its place by
        # using grid
        button2.grid(row=2, column=1, padx=10, pady=10)


# third window frame page2
class Deposit(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        date_select = self.controller.get_page(DateSelect)

        date_label = ttk.Label(self, text=date_select.cal.get_date())
        date_label.grid(row=3, column=4, padx=10, pady=10)

        label = ttk.Label(self, text="Deposit", font=LARGEFONT)
        label.grid(row=0, column=4, padx=10, pady=10)

        # button to show frame 2 with text
        # layout2
        button1 = ttk.Button(self, text="Return To Date Select",
                             command=lambda: controller.show_frame(DateSelect))

        # putting the button in its place by
        # using grid
        button1.grid(row=1, column=1, padx=10, pady=10)

        # button to show frame 3 with text
        # layout3
        button2 = ttk.Button(self, text="Logout",
                             command=lambda: controller.show_frame(Login))

        # putting the button in its place by
        # using grid
        button2.grid(row=2, column=1, padx=10, pady=10)

# Driver Code
app = tkinterApp()
app.mainloop()

你看过这个答案中的一些链接了吗?