Python Forum
Help me understand this... (Classes and self).
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help me understand this... (Classes and self).
#13
Okay new question, still related to classes and tkinter though:
How do I display the contents of a frame from object A, in another frame from object B?
Example...

import tkinter as tk
import tkinter.ttk as ttk


class MainFrame(tk.Tk):
    '''
    _Target_ for insertion of widgets to display from another frame constructed
    from another class.
    '''

    def __init__(self, parent=None):
        super().__init__(parent)
        self.build_mb()
        self.build_nb()

    def build_mb(self):
        menu_bar = tk.Menu(self)
        self.config(menu=menu_bar)
        options_menu = tk.Menu(menu_bar, tearoff=0)
        menu_bar.add_cascade(label="Options", menu=options_menu)
        options_menu.add_command(label="Quit", command=self.destroy)

    def build_nb(self):
        nb = ttk.Notebook(self)
        nb.grid()
        tab_one = tk.Frame(nb) # <-- target actual # #
        nb.add(tab_one, text='Entries')
        tab_two = tk.Frame(nb)
        nb.add(tab_two, text='Unused')


class TabOne(tk.Frame):
    '''
    _This_ is the frame to display in "tab_one" of the MainFrame object.
    '''

    def __init__(self, parent=None):
        super().__init__(parent)
        self.build_tab()

    def build_tab(self):
        entry = ttk.Entry()
        entry.grid(column=0, row=0)
        submit = ttk.Button(text='Submit')
        submit.grid(column=1, row=0)


def run_main():
    main_nb = MainFrame()
    tab1 = TabOne(main_nb) #This doesn't work; "main_nb.tab_one" (or whatever) throws error.
    main_nb.mainloop()

if __name__ == '__main__':
    run_main()
Is it possible to get the widgets in the frame named tab1 to display in the notebook this way, or must it all be constructed together?
Reply


Messages In This Thread
RE: Help me understand this... (Classes and self). - by Ceegen - Mar-30-2019, 07:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand classes menator01 7 3,478 Oct-27-2019, 04:26 PM
Last Post: menator01
Question Helping understand classes Miraclefruit 10 6,536 Nov-27-2017, 01:58 PM
Last Post: Windspar
  Using classes? Can I just use classes to structure code? muteboy 5 5,236 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020