Adding tkinter modules to notebook tabs

Christian Gollwitzer auriocus at gmx.de
Sat Apr 4 16:53:42 EDT 2020


Am 04.04.20 um 22:31 schrieb Rich Shepard:
> On Sat, 4 Apr 2020, Christian Gollwitzer wrote:
> 
>> I'm not sure I fully understand it, because a "module" is not defined 
>> in the language of tkinter.
> 
> Christian,
> 
> True, but it is in Python: a file ending in .py which, in this case,
> contains a class of tkinter widgets.
> 
>> Is it correct, you want to know how to place more than one widget - a
>> arrangement of widgets - onto a notebook tab?
> 
> Yes, as they are defined and laid out in separate files/modules.
> 
>> This is achieved by a ttk.Frame() widget, which holds all the other 
>> stuff, and then you place the frame onto the notebook:
>>
>> n=ttk.Notebook()
>> f1=ttk.Frame()
>> # put all your stuff into f1
>> n.add(f1, text="Module1")
>> # repeat for your other "modules"
> 
> This looks to me like it's adding the text, "Module1" to the tab.

Yes and no. It puts the empty frame in the notebook and labels it with 
"Module1" so that when you click the label, the frame is raised. Of 
course, not very interesting, because the frame is empty.

> 
> Here:
> 
> nb = ttk.Notebook(Main)
>      ...
>      page3 = ttk.Frame(nb)
>      ...
>      # add titles to tabs
>      nb.add(page3, text='Biota')
>      ...
> 
> This puts the text on each notebook tab, correct?

It adds an empty frame, as above.

> 
> If so, how do I add
> 
> class BiotaDataForm(Tk.Frame):
> 
> which defines all the widgets in the module in views/biota.py into the body
> of the notebook's page3?

Add that thing instead of the frame.

blabla=BioDataForm() # whatever args it needs, maybe parent=nb

nb.add(blabla, text="Biodata")

	Christian

PS: I suggest to change all Tk widgets to ttk widgets for optical 
reasons, but that's a side note.



More information about the Python-list mailing list