0

I'm trying to build Tabs in my program. Each Tab has difrrent functions,Labels,Entries and so on... My idea is to create something like a mainTab file that create all the tabs. Is it possible to inherit from the mainTab file and create Tab classes as much as i want? I want to separate each Tab class, because each class has alot methods and i dont want to write the whole tab classes on one file. Is it possible to create? If it is, how can i create instanse of the tab and connect it to the tab_control?

For example: This is what i wrote, all tabs in one classes (which i don't want to create it like that)

class CreateLayout:
def __init__(self, window):
    self.window = window

   window.title('TITLE')
    window.geometry('800x600')
    # Create Tab Control
    tab_control = ttk.Notebook(window)

    # Tab1
    tab1 = ttk.Frame(tab_control)
    tab_control.add(tab1, text='Hashtag')
    tab_control.pack(expand=1, fill="both")

    # Tab2
    tab2 = ttk.Frame(tab_control)
    tab_control.add(tab2, text='Following me')
    tab_control.pack(expand=1, fill="both")

I want to create sub classes(Tabs) that inherit from CreateLayout class, and on each sub class write it own Tab logic with methods, layout and more...

I hope my question is clear, if not please let me know.

Bottom line, what i want to create is classes that inherit from the CreateLayout and in the CreateLayout build the tabs.

For example

class CreateLayout:
def __init__(self, window):
    self.window = window

tab_control = ttk.Notebook(window)

tab1 = Tab1(window)
tab_control.add(tab1, text='Hashtag')

something like that.

E.Bolandian
  • 553
  • 10
  • 35
  • You have to change `Tab1(window)` to `Tab1(tab_control)`. Relevant: [Correctly extend a tkinter widget using inheritance](https://stackoverflow.com/a/60322108/7414759), in your case you have to inherit from `Frame`. – stovfl Mar 25 '20 at 12:09
  • And thats it? thats all i need to do on the `main` file to create a tab? – E.Bolandian Mar 25 '20 at 12:23
  • 1
    ***"And thats it?"***: Yes, **1.** The "Tab" have to be a container widget, common `Frame`. **2.** The parent of the "Tab" have to be the `Notebook` widget. – stovfl Mar 25 '20 at 12:41

1 Answers1

1

Is it possible to inherit from the mainTab file and create Tab classes as much as i want?

Yes, it is possible. Tkinter doesn't impose any restrictions on inheritance. In fact, tkinter makes this really easy since it's possible to inherit from tkinter widgets. The key is for your tabs to inherit from tk.Frame so that they can be treated as a normal widget.

For example, the hashtag tab definition might look something like this:

class HashtagTab(tk.Frame):
    def __init__(self, master):
        super().__init__(master)

        label = tk.Label(self, text="This is the hashtag tab")
        label.pack(padx=20, pady=20)

Other tabs would follow the same pattern. You can put whatever code you want in this class as long as all widgets are descendants of self, and the widgets in this class will not interfere with widgets in any other class.

In your main code, you would create an instance of this class and add it to the tab control just like you would any other frame:

tab_control = ttk.Notebook(window)
...
hashtag_tab = HashtagTab(tab_control)
following_tab = FollowingTab(tab_control)
...
tab_control.add(hashtag_tab, text="Hashtag")
tab_control.add(following_tab, text="Following")
...
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • On class HashtagTab, i should only inherit from `tk.Frame`? What about the main code that is on another file, should i inherit from him too? – E.Bolandian Mar 25 '20 at 14:58
  • @E.Bolandian: no, you should not inherit from the main code. Inheritance isn't for sharing data, inheritiance defines a "_is a_" relationship - `HashTab` _is a_ `Frame`, but it is _not_ a `CreateLayou`. – Bryan Oakley Mar 25 '20 at 15:01