python - 如何将用户输入设置为另一个框架中 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
当我切换到第二帧时,我的第二个框一直是空白的。
解决方案
当变量为空字符串时,您正在初始化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()
推荐阅读
- python - 是否有一个功能或一段代码可以使它的炮塔可以预测目标在哪里并在该地点射击?(线性预测)
- continuous-integration - CI过程中的Apple ID双因素身份验证
- sql - 有没有办法根据这些行的内容按日期识别要使用的一组行?
- python - 使复杂的列表理解成为条件
- karate - 为什么 maven Central 中没有 karate-testing 0.9.1。我正在尝试利用“重试直到”的新功能之一
- reactjs - React 推荐的引导库
- java - 如何将 JAXB 运行时库与 OpenJDK 11 捆绑在一起?
- python - 在 Python 上移动百分比 - Pandas
- python - 使用 Bokeh 进行标记聚类
- javascript - 将新日期传递给 d3 弹出窗口