Search code examples
pythontkinter

How do I access a class component from another component that is not being inherited, in Tkinter Python?


How do I access a class component from another component that is not being inherited?

I am creating an application using Tkinter Python. I am using classes as frames for each component.

How do I reference one component as the same instance without creating a new component, while the classes do not inherit another?

"""
Name: John Roby
Date: 10/21/2024
Description: A password manager gui app.
"""

import tkinter as tk
from tkinter import ttk
from Components.Header import Header
from Components.SavedPasswords import SavedPasswords
from Components.CreatePassword import CreatePassword


# App class inherits tkinter
class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title('Password Manager')
        self.minsize(400, 800)
        self.maxsize(400, 800)
        # Add Components
        self.header = Header(self)
        self.savedpasswords = SavedPasswords(self)
        self.createpassword = CreatePassword(self)
       



if __name__ == "__main__":
    App().mainloop()
    

I am trying to reference the other component in this file.

Custom Class Components Header

"""
Description: Header.py - Custom Component Class Header.
"""

import tkinter as tk
from tkinter import ttk

# Custom Component Header
class Header(ttk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.pack()
        # Grid Layout
        self.rowconfigure(0, weight=1)
        self.columnconfigure((0,1,2,3,4), weight=1, uniform='a')
        # Header Widgets
        self.create_header_widgets()


    def create_header_widgets(self):
        """Creates and places widgets
        that make up Header component"""
        # Widgets
        search = ttk.Entry(self)
        button = ttk.Button(self, text="Search")

        # Place
        search.grid(row=0, column=2)
        button.grid(row=0, column=3)

SavedPasswords

"""
Description: SavedPasswords.py - Custom Component Class SavedPasswords.
"""

import tkinter
from tkinter import CENTER, E, W, ttk

# Custom Component Class SavedPasswords
class SavedPasswords(ttk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.pack()
        # SavedPasswords Widgets
        self.password_table = ttk.Treeview(self)
        # Define Columns
        self.password_table['columns'] = ("Website", "Username", "Password")
        self.password_table.column("#0", width=0, minwidth=0)
        self.password_table.column("Website", anchor=W, width=120)
        self.password_table.column("Username", anchor=CENTER, width=120)
        self.password_table.column("Password", anchor=E, width=120)
        # Create Headings
        self.password_table.heading("#0", text="")
        self.password_table.heading("Website", text="Website", anchor=W)
        self.password_table.heading("Username", text="Username", anchor=CENTER)
        self.password_table.heading("Password", text="Password", anchor=E)
        # Insert Data
        self.password_table.insert(parent='', index='end', iid=0, text="", values=("Facebook.com", "johnjroby@gmail.com", "Password123"))
        
        self.password_table.pack(pady=10)

CreatePassword

"""
Description: SavedPasswords.py - Custom Component Class SavedPasswords.
"""

import tkinter
from tkinter import CENTER, E, W, ttk
from Components.SavedPasswords import SavedPasswords

# Custom Component Class CreatePassword
class CreatePassword(ttk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.pack()
        # Grid Layout
        self.rowconfigure((0,1,2,3,4,5), weight=1)
        self.columnconfigure((0,1,2,3,4,5), weight=1, uniform='a')
        
        # CreatePassword Widgets
        self.website_label = ttk.Label(self, text="Website")
        self.username_label = ttk.Label(self, text="Username")
        self.password_label = ttk.Label(self, text="Password")
        
        self.website_entry = ttk.Entry(self)
        self.username_entry = ttk.Entry(self)
        self.password_entry = ttk.Entry(self)
        
        self.save_button = ttk.Button(self, text="Save", command=self.saveValues)
        
        # Place
        self.website_label.grid(row=0, column=0)
        self.website_entry.grid(row=1, column=0)
        
        self.username_label.grid(row=2, column=0)
        self.username_entry.grid(row=3, column=0)
        
        self.password_label.grid(row=4, column=0)
        self.password_entry.grid(row=5, column=0)
        
        self.save_button.grid(row=5, column=1)

    def saveValues(self):
        website = self.website_entry.get()
        username = self.username_entry.get()
        password = self.password_entry.get()

        self.password_table.insert(values=(website, username, password))


Solution

  • I added password_table as an attribute to the CreatePassword class.

    class CreatePassword(ttk.Frame):
        def __init__(self, parent, password_table):
            super().__init__(parent)
    

    And satisfied it in the App() component.

    # App class inherits tkinter
    class App(tk.Tk):
        def __init__(self):
            super().__init__()
            self.title('Password Manager')
            # Add Components
            self.header = Header(self)
            self.savedpasswords = SavedPasswords(self)
            self.createpassword = CreatePassword(self, self.savedpasswords.password_table)
    

    This seems to work and I am able to add the values to the table.