python-3.xtkintertkinter-entry

How to entry.get() from one subclass ttk.frame to another subclass ttk.frame


I have two subclass frames, MainFrame and SubFrame. The SubFrame has an entry widget (entry1) to enter a time, the default entry is is 00:00. From the MainFrame I want to get the time that has been entered in the entry1 box in the SubFrame. How do I go about getting the entry widget data in SubFrame from MainFrame?

 #!/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' # build the style name using object ID
        super().__init__(container, style=style_name) # apply the style

        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):
        print(self.other.entry1.get())
        
class SubFrame(ttk.Frame):
    def __init__(self, container):
        style_name = f'{id(self)}.TFrame' # build the style name using object ID
        super().__init__(container, style=style_name) # apply the style

        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' } # highlightcolor':"red"

        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))
        self.entry1.configure(style)
 
    ## works if called from within subclass
    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)

        self.Frame1.other = 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.resizable(False, False)
        self.title("TABS")
           

if __name__ == '__main__':
    
    app = App()

    sFrame = SubFrame(app)
    MainFrame(app)
    SubFrame(app)
    
    app.mainloop()

Solution

  • You have a reference before declaration problem with Frame1 and Frame2.

    Frame1 had no reference to Frame2 since it didn't exist when Frame1 was created.

    Give Frame1 a reference to Frame2 and your problem is solved.

    For example you might try the following code:

    
    self.Frame1 = MainFrame(self.notebook)
    self.Frame2 = SubFrame(self.notebook)
    
    # create a link from frame2 to frame1 as other
    self.Frame1.other = self.Frame2
    

    Then change MainFrame.get_time to:

        def get_time(self):
            print(self.other.entry1.get())