首页 > 解决方案 > 如何将用户输入设置为另一个框架中 Tkinter Entry 小部件的默认文本?

问题描述

我有两个框架可以切换。在每一帧中,都有一个Entry盒子。如何Entry将第一帧第一个框中的输入设置为第二帧第二个框中的默认文本Entry

from tkinter import *


class Root(Tk):

    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        container = Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

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

        self.show_frame("PageOne")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

    def quit(self):
        self.destroy()

text = ''


class PageOne(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller

        global text

        entry_box_1 = Entry(self, width=40)
        entry_box_1.pack()
        text = entry_box_1.get()

        quit_button = Button(self, text="Quit Program",
                    command=lambda: controller.quit())
        next_button = Button(self, text="Next",
                    command=lambda: controller.show_frame('PageTwo'))

        next_button.pack()
        quit_button.pack()


class PageTwo(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller

        entry_box_2 = Entry(self, width=40)
        entry_box_2.insert(END, text)
        entry_box_2.pack()

        quit_button = Button(self, text="Quit Program",
                            command=lambda: controller.quit())
        back_button = Button(self, text="Back",
                            command=lambda: controller.show_frame("PageOne"))

        back_button.pack()
        quit_button.pack()

if __name__ == "__main__":
    root = Root()
    root.mainloop()

Entry当我切换到第二帧时,我的第二个框一直是空白的。

标签: pythontkinter

解决方案


当变量为空字符串时,您正在初始化PageTwo框架,text并且当您切换到此框架时,您没有向entry_box_2.

insert我建议您每次切换到PageTwo框架时调用该方法。您可以通过创建一个类似于show_frame_with_default_text以下代码中的方法的新切换方法来做到这一点。

from tkinter import *


class Root(Tk):

    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        container = Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

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

        self.show_frame("PageOne")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

    def show_frame_with_default_text(self, page_name, text):
        frame = self.frames[page_name]
        frame.entry_box.delete(0, END)
        frame.entry_box.insert(0, text)
        frame.tkraise()

    def quit(self):
        self.destroy()


class PageOne(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller

        entry_box_1 = Entry(self, width=40)
        entry_box_1.pack()

        quit_button = Button(self, text="Quit Program",
                    command=lambda: controller.quit())
        next_button = Button(self, text="Next",
                    command=lambda: controller.show_frame_with_default_text('PageTwo', entry_box_1.get()))

        next_button.pack()
        quit_button.pack()


class PageTwo(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller

        self.entry_box = Entry(self, width=40)
        self.entry_box.pack()

        quit_button = Button(self, text="Quit Program",
                            command=lambda: controller.quit())
        back_button = Button(self, text="Back",
                            command=lambda: controller.show_frame("PageOne"))

        back_button.pack()
        quit_button.pack()


if __name__ == "__main__":
    root = Root()
    root.mainloop()

推荐阅读