-2

I found a way makeing it easy to automatically generate multiple Tabs with the ttk notebook. As a "solution" to: http://stackoverflow.com/questions/39175898/automatic-multiple-tab-generation-with-tkinter-notebook?noredirect=1#comment65695477_39175898 But now I have a problem to fill the Tabs with individual content namely widgets. What do I need for "???" in the commented out part? I would appreciate it very much if you show me how to solve this.

from tkinter import *
from tkinter import ttk

###
class MyTab(Frame):

    def __init__(self, root, name):
        Frame.__init__(self, root)

        self.root = root
        self.name = name


###
class Application():

    def __init__(self):

        self.tabs = {'ky': 1}

        print(self.tabs['ky'])

        self.root = Tk()
        self.root.minsize(300, 300)

###     Tab generation
        self.notebook = ttk.Notebook(self.root, width=800, height=550)

        tab_names = ["info", "S00", "S01", "S02", "S03", "S04", "S05", "S06", "S07", "S08", "help"]

        for i in range(0, len(tab_names)):
            tab = MyTab(self.notebook, tab_names[i])
            self.notebook.add(tab, text=tab_names[i])

        self.button = Button(self.root, text='next ->', command=self.next_Tab).pack(side=BOTTOM)

###     info Tab Widgets
        #self.btn1 = Button(???, text='Info Button', command=self.next_Tab).pack(side=RIGHT)

###     S00 Tab Widgets
        #self.btn2 = Button(???, text="test_btn")
        #self.btn2.pack()

###     S01 Tab Widgets and so on...

        self.notebook.pack(side=TOP)

    def next_Tab(self):
        print("next Tab -> not yet defined")

    def run(self):
        self.root.mainloop()

###

Application().run()
quest4stack
  • 13
  • 1
  • 3
  • Please re-write your question. It is not clear what your asking. Thanks. – Christian Dean Aug 28 '16 at 01:46
  • @Mr.goosberry thank you for your reply. I am trying to place individual widgest in the different tabs. (So that they are only visible when the regarding single Tab is activ/in the foreground.) I could not find an example where it's done in combination with a loop generating the tabs. Who knows how the code for such a widget might look like? I don't know what to write for the "???" in the commented out section. Hopefully this clarified it a bit more. – quest4stack Aug 28 '16 at 10:48

1 Answers1

1

The rule is quite simple: to place a widget in another widget, you need a reference to the other widget. In your case, the simple thing to do is create a dictionary to hold references to your frames:

tabs = {}
for i in range(0, len(tab_names)):
    tab = MyTab(self.notebook, tab_names[i])
    self.notebook.add(tab, text=tab_names[i])
    tabs[tab_names[i]] = tab
...
self.btn1 = Button(tabs["info"], ...)

By the way, you can make your loop more readable (and more "pythonic") by directly iterating over the list of tab names rather than iterating over the index values:

tabs = {}
for tab_name in tab_names:
    tab = MyTab(self.notebook, tab_name)
    self.notebook.add(tab, text=tab_name)
    tabs[tab_name] = tab
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685