如何在具有多个页面的 tkinter 应用程序中获取按钮单击时的条目值?

How to obtain the entry value on button click in a tkinter app with multiple pages?

我想在单击按钮 Run 时从 Tkinter 应用程序的 Page2 获取当前条目值。

请帮忙!

我的代码:

import tkinter as tk
from tkinter import ttk
from tkinter import RAISED


LARGEFONT =("Verdana", 35)

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)

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

        container2 = tk.Frame(self, relief=RAISED, borderwidth=2)
        container2.pack(side="bottom",fill="both", expand=True)
        container2.grid_rowconfigure(0, weight = 1)
        container2.grid_columnconfigure(0, weight = 1)

        def runStadiumpy():
            pass
            #get all entry value from page 2


        runButton = ttk.Button(container2, text="Run", command=runStadiumpy)
        runButton.pack(side="left", padx=5, pady=5)

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

        # iterating through a tuple consisting
        # of the different page layouts
        for F in (StartPage, Page1, Page2):

            frame = F(container, self)

            # initializing frame of that object from
            # startpage, page1, page2 respectively with
            # for loop
            self.frames[F] = frame

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

        self.show_frame(StartPage)

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

# first window frame startpage

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        # label of frame Layout 2
        label = ttk.Label(self, text ="Startpage", font = LARGEFONT)

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

        button1 = ttk.Button(self, text ="Page 1",
        command = lambda : controller.show_frame(Page1))

        # 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 ="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 Page1(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text ="Page 1", font = LARGEFONT)
        label.grid(row = 0, column = 2, padx = 10, pady = 10)

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

        # 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 ="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)




# third window frame page2
class Page2(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text ="Page 2", font = LARGEFONT)
        label.grid(row = 0, column = 2, padx = 10, pady = 10)

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

        # 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 ="Startpage",
                            command = lambda : controller.show_frame(StartPage))

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

        lbl1 = ttk.Label(self, text="Question:")
        lbl1.grid(row = 3, column = 1, padx = 10, pady = 10)

        entry1 = ttk.Entry(self)
        entry1.grid(row = 3, column = 2, padx = 10, pady = 10)
        entry1.insert(0,"abc")


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

这对你有用:-

def runStadiumpy():
    value = entry1.get()
    return value

        

为您在 class Page2 中的条目保留参考,例如:

class Page2(tk.Frame):
    def __init__(self, parent, controller):

        ......

        self.entry1 = ttk.Entry(self)
        self.entry1.grid(row = 3, column = 2, padx = 10, pady = 10)
        self.entry1.insert(0,"abc")

由于您将这些帧保存在 self.frames 中,您可以通过 self.frames[Page2] 访问 Page2。为您的条目添加引用后,使用 self.frames[Page2].entry1 获取直接输入。

所以在添加上面的代码之后,在函数 runStadiumpy():

中使用下面的代码
def runStadiumpy():
    print(self.frames[Page2].entry1.get())

然后你会得到第2页的内容