Reviewing different examples of notebook implementation, it has caught my attention that in this example:
from tkinter import ttk
import tkinter as tk
from tkinter.scrolledtext import ScrolledText
def demo():
root = tk.Tk()
root.title("ttk.Notebook")
nb = ttk.Notebook(root)
# adding Frames as pages for the ttk.Notebook
# first page, which would get widgets gridded into it
page1 = ttk.Frame(nb)
# second page
page2 = ttk.Frame(nb)
text = ScrolledText(page2)
text.pack(expand=1, fill="both")
nb.add(page1, text='One')
nb.add(page2, text='Two')
nb.pack(expand=1, fill="both")
root.mainloop()
if __name__ == "__main__":
demo()
the page1 and page2 frames are not "explicitly" managed by a layout manager, in this case pack(), and still work correctly. This example, I think, is taken from an official documentation.
In all the documentation that I read, it is emphasized that if the widgets are not managed with a layout manager, they do not become visible.
If I wanted to manage page1 and page2 with grid(), how should I indicate the columns for each of them?
You can manage the content of each Frame with the geometry manager you want. One thing is what you position within the widget
raiz
of each tab (in the example a Frame, which it usespack
to position its children) and another is how the root widget itself (the Frame) is positioned in the NoteBook panel.Internally the root widget of each tab is implicitly positioned by calling the method
add
and positioned on the NoteBook panel as if usinggrid
. Keep in mind that we can only have one root widget for each tab, therefore a grid with a single row and column.In your example the "family tree" of the app is:
In fact, it
add
has an argumentsticky
that acts identically to the argument ofgrid
:As when it is used explicitly
grid
, itsticky
allows to indicate where the widget should be "pulled", allowing to define what should happen with the remaining space of the panel, if once the root widget has been expanded to contain its children, there is space left over in the NoteBook panel.We cannot modify this, the module
ttk
in this case is limited to acting as a wrapper over tcl/tk usedtkinter.call
to calladd
thettk::notebook
.As I mentioned,
add
it implicitly positions the root widget for that tab, usually aFrame
, but it can be any widget:Now, we can position within the root widget (if it allows it, that's why a is commonly used
Frame
) the widgets we want and use the geometry manager we want, in my example I usegrid
:But you can use whichever you want:
What you can't define is how the root widget, in this case our frame in the panel, is positioned beyond manage
sticky
. This does not imply any limitation, since by positioning aFrame
as root we can place and position the child widgets that we want with the geometry manager that we want and the nesting that we want in the tab.