Python 如何在另一个框架中将用户输入设置为Tkinter条目小部件的默认文本?
我有两帧要切换。在每个帧中,都有一个Python 如何在另一个框架中将用户输入设置为Tkinter条目小部件的默认文本?,python,tkinter,Python,Tkinter,我有两帧要切换。在每个帧中,都有一个条目框。如何将第一帧中第一个条目框中的输入设置为第二帧中第二个条目框的默认文本 from tkinter import * class Root(Tk): def __init__(self, *args, **kwargs): Tk.__init__(self, *args, **kwargs) container = Frame(self) container.pack(side="top",
条目
框。如何将第一帧中第一个条目
框中的输入设置为第二帧中第二个条目
框的默认文本
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()
当我切换到第二帧时,我的第二个
条目
框一直为空 当text
变量为空字符串时,您正在初始化PageTwo
框架,并且在切换到此框架时,您没有向输入框2
插入任何值
我建议您在每次切换到PageTwo
框架时调用insert
方法。您可以通过创建一个新的切换方法来实现这一点,该方法类似于以下代码中的show\u frame\u with\u default\u 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()
“在第一个输入框中设置我的输入…用于第二个输入框”:您可以使用回调或实现def do_update(…
),它在每个帧
开关上调用。