Home > Article > Backend Development > How to Access Variable Data from an Entry Widget in a Multi-Page Tkinter Application?
Question:
In a multi-page Tkinter application, how do you access variable data from an Entry widget in one page within another page? Every attempt raises an exception.
How to Leverage the Controller:
Given the app's existing controller concept, use it to facilitate communication between pages.
Save a controller reference in each page:
class PageOne(ttk.Frame): def __init__(self, parent, controller): self.controller = controller
Create a controller method to return a page given its class name:
class MyApp(Tk): def get_page(self, classname): for page in self.frames.values(): if str(page.__class__.__name__) == classname: return page
Access public members of other pages:
class PageTwo(ttk.Frame): def print_it(self): page_one = self.controller.get_page("PageOne") value = page_one.some_entry.get()
Storing Data in the Controller:
To avoid tight coupling, consider storing data in the controller:
Create a data structure in the controller (e.g., dictionary):
class MyApp(Tk): def __init__(self): self.app_data = {"name": StringVar()}
Modify pages to reference the controller when creating widgets:
class PageOne(ttk.Frame): def __init__(self, parent, controller): self.controller=controller self.some_entry = ttk.Entry(self, textvariable=self.controller.app_data["name"])
Access data from the controller:
class PageTwo(ttk.Frame): def print_it(self): value = self.controller.app_data["name"].get()
The above is the detailed content of How to Access Variable Data from an Entry Widget in a Multi-Page Tkinter Application?. For more information, please follow other related articles on the PHP Chinese website!