How to Access Variable Data from an Entry Widget in a Multi-Page Tkinter Application?

Patricia Arquette
Release: 2024-11-04 04:52:29
Original
223 people have browsed it

How to Access Variable Data from an Entry Widget in a Multi-Page Tkinter Application?

Extracting Variable Data from a Class in a Multi-Page 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.

  1. Save a controller reference in each page:

    class PageOne(ttk.Frame):
        def __init__(self, parent, controller):
            self.controller = controller
    Copy after login
  2. 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
    Copy after login
  3. 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()
    Copy after login

Storing Data in the Controller:

To avoid tight coupling, consider storing data in the controller:

  1. Create a data structure in the controller (e.g., dictionary):

    class MyApp(Tk):
        def __init__(self):
            self.app_data = {"name": StringVar()}
    Copy after login
  2. 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"])
    Copy after login
  3. Access data from the controller:

    class PageTwo(ttk.Frame):
        def print_it(self):
            value = self.controller.app_data["name"].get()
    Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!