Request Help With ttk.Notebook Tabs

Terry Reedy tjreedy at udel.edu
Tue Feb 21 14:38:35 EST 2017


On 2/21/2017 1:02 PM, Wildman via Python-list wrote:
> Python 3.4.2
> Linux platform
>
>
> I am working on a program that has tabs created with ttk.Notebook.
> The code for creating the tabs is working but there is one thing I
> have not been able to figure out.  As is, the tabs are located up
> against the lower edge of the caption bar.  I would like to have
> them a little lower to make room above the tabs for other widgets
> such as labels and/or command buttons.  Here is the code I use to
> create the window and tabs...
>
> class Window(tk.Frame):
>     def __init__(self, master=None):
>         tk.Frame.__init__(self, master)
>         master.title("My Program")
>         nb = ttk.Notebook(root, width=600, height=340)
>         tab1 = tk.Frame(nb)
>         tab2 = tk.Frame(nb)
>         nb.add(tab1, text="Tab1")
>         nb.add(tab2, text="Tab2")
>         nb.pack(expand=1, fill="both")

This is too minimal.  If I copy, paste, and run the above, it will fail. 
I will not guess what other code (which you should minimize) you 
actually had to make this run.  See https://stackoverflow.com/help/mcve, 
which is *great* advice that most SO unfortunately ignore.

> I have tried the grid and place methods to move the tabs down but
> neither works.  I have tried the methods both before and after the
> pack method.  Here is an example of what I have tried...
>
>         nb.grid(row=2, column=2)
> or
>         nb.place(x=10, y=10)
>
> Would be appreciated if anyone could provide any guidance.

I have not specifically used Notebook, but

0. You MUST NOT mix geometry methods within a particular toplevel or 
frame.  From what you wrote (without complete code), you might have.

1. All widgets that go within a particular container widget must have 
that container as parent.  Although you put the notebook code in the 
window(frame) init, its parent is root, so it will appear outside the 
window, if indeed the window is made visible (which it is not in the 
code you posted.  So either get rid of the window(frame) code or put the 
notebook in the windows instance

          nb = ttk.Notebook(self, width=600, height=340)

The former is easier for minimal proof-of-concept testing, the latter 
may be better for final development and maintenance.

2. Packing is order dependent.

3. Gridding is not order dependent, but empty rows and column take no space.

In any case, the following works for me on 3.6 and Win10.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

e = ttk.Label(root, text='Widget outside of notebook')

nb = ttk.Notebook(root, width=600, height=340)
tab1 = ttk.Frame(nb)
tab2 = ttk.Frame(nb)
ttk.Label(tab1, text='Text on notebook tab 1').pack()
ttk.Label(tab2, text='Tab 2 text').pack()
nb.add(tab1, text="Tab1")
nb.add(tab2, text="Tab2")

e.grid(row=0)
nb.grid(row=1)

#root.mainloop()  # not needed when run from IDLE

-- 
Terry Jan Reedy




More information about the Python-list mailing list