如何从ttk.Frame子类MainFrame获取另一子类SubFrame的Entry内容
如何在MainFrame中获取SubFrame的Entry输入值
要实现MainFrame获取SubFrame中Entry的输入值,有几种直接可行的方式,下面结合你的代码给出具体方案:
方法一:建立Frame实例间的直接引用
这是最直观的方式,在主应用类中给MainFrame实例添加指向SubFrame实例的引用,然后在MainFrame中通过该引用访问SubFrame的输入控件或其绑定的变量。
修正你的现有代码:
你的代码思路本身是对的,但存在两处问题:
__main__块中额外创建的SubFrame和MainFrame实例与Notebook中的实例无关,会导致界面混乱,需要删除这部分代码self.entry1.configure(style)写法错误,因为style是字典,需要用**style来传递关键字参数
修正后的完整代码:
#!/usr/bin/python3.9 import tkinter as tk from tkinter import ttk class MainFrame(ttk.Frame): def __init__(self, container): style_name = f'{id(self)}.TFrame' super().__init__(container, style=style_name) s = ttk.Style() s.configure(style_name, background='red') self.labelA = ttk.Label(self, text="This is on MainFrame") self.labelA.grid(column=1, row=2, padx=30, pady=30) self.buttonA = tk.Button(self, text="get from subframe", command=self.get_time) self.buttonA.grid(column=1, row=3) def get_time(self): # 通过引用的SubFrame实例获取输入值,推荐用StringVar的get()方法 print(self.sub_frame.entry1_var.get()) # 也可以直接获取Entry的内容:print(self.sub_frame.entry1.get()) class SubFrame(ttk.Frame): def __init__(self, container): style_name = f'{id(self)}.TFrame' super().__init__(container, style=style_name) s = ttk.Style() s.configure(style_name, background='blue') self.labelB = ttk.Label(self, text="This is on SubFrame") self.labelB.grid(column=1, row=2, padx=30, pady=30) self.buttonB = tk.Button(self, text="get within class", command=self.get_time) self.buttonB.grid(column=1, row=3) ## ENTRY STYLE style = {'fg': 'black', 'bg': 'white', 'font': 'Helvetica 14 bold', 'width':'6', 'bd':'2', 'highlightbackground':"black", 'justify':'center', 'relief':'sunken', 'insertontime': '0' } self.entry1_var=tk.StringVar() self.entry1_var.set("00:00") self.entry1=tk.Entry(self, textvariable=self.entry1_var, width=5, justify="center") self.entry1.grid(column=2, row=3, padx=(0, 0), pady=(0,10)) # 修正:用**style传递字典参数 self.entry1.configure(**style) def get_time(self): data = self.entry1_var.get() print(data) class App(tk.Tk): def __init__(self): super().__init__() s = ttk.Style() unfocus = "gray" focus = "#F2C84B" s.theme_create( "custom", parent="alt", settings={ "TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0] } }, "TNotebook.Tab": { "configure": {"padding": [5, 1], "background": unfocus }, "map": {"background": [("selected", focus)], "expand": [("selected", [1, 1, 1, 0])] } } } ) s.theme_use("custom") self.notebook = ttk.Notebook(self) self.Frame1 = MainFrame(self.notebook) self.Frame2 = SubFrame(self.notebook) # 给MainFrame添加指向SubFrame的引用 self.Frame1.sub_frame = self.Frame2 self.notebook.add(self.Frame1, text='MainFrame') self.notebook.add(self.Frame2, text='SubFrame') self.notebook.pack(expand=1, fill="both") self.geometry('800x500') self.title("TABS") if __name__ == '__main__': app = App() # 删除多余的Frame实例创建代码 app.mainloop()
方法二:使用解耦的方式(推荐)
如果想让两个Frame类之间的依赖更低,可以让SubFrame提供一个获取输入值的方法,或者在App类中共享StringVar变量:
方式1:SubFrame暴露获取值的方法
在SubFrame中添加一个方法:
def get_input_time(self): return self.entry1_var.get()
然后在MainFrame的get_time方法中调用:
def get_time(self): print(self.sub_frame.get_input_time())
方式2:共享StringVar
在App类中创建StringVar,然后传递给SubFrame:
class App(tk.Tk): def __init__(self): super().__init__() # ... 其他代码 self.time_var = tk.StringVar(value="00:00") self.Frame1 = MainFrame(self.notebook, time_var=self.time_var) self.Frame2 = SubFrame(self.notebook, time_var=self.time_var) # ... 其他代码
然后修改SubFrame的__init__:
def __init__(self, container, time_var): super().__init__(container, style=style_name) self.entry1_var = time_var self.entry1=tk.Entry(self, textvariable=self.entry1_var, width=5, justify="center") # ... 其他代码
MainFrame的get_time直接访问共享变量:
def __init__(self, container, time_var): super().__init__(container, style=style_name) self.time_var = time_var # ... 其他代码 def get_time(self): print(self.time_var.get())
这种方式让两个Frame不需要直接引用,耦合度更低,适合复杂应用。
内容的提问来源于stack exchange,提问作者bob_the_bob
相关产品推荐
相关产品推荐

