Reputation: 35
I would like to know how you would set a tkinter notebook tab in a specific place. For example, I have this tab that allows you to clone other tabs, but I want it to stay at the end. The code I tried does not seem to work though, as it generates an tkinter.TclError : "Slave index (whatever number) out of bounds".
`
from tkinter import *\
from tkinter import ttk, messagebox as msg\
from widgets.translate import Translator\
from widgets.explore import FileExplorer\
root = Tk()\
root.title('Widgets')
tabs = ttk.Notebook(root, width=900, height=350)
tabs.pack(fill='both', expand=1, pady=(5, 0))
tr = ttk.Frame(tabs)
tr_frame = ttk.Frame(tr)
tr_frame.pack(fill=Y)
tabs.add(tr, text='Translator')
Translator(tr_frame)
explorers=0
translators=0
def fileexplore_tab(index=2):
global explorers
fl_explore = ttk.Frame(tabs)
tabs.insert(index, fl_explore, text='File Explorer')
FileExplorer(fl_explore)
explorers += 1
fileexplore_tab(2)
fileexplore_tab(len(tabs.tabs())-1)
print(len(tabs.tabs()))
root.mainloop()
`
Upvotes: 0
Views: 1861
Reputation: 385900
How do you insert a tab in a specific place in a tkinter ttk notebook?
The insert
method allows you to specify an integer id. For example, the following code creates four tabs, then inserts a fifth in the middle.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
notebook = ttk.Notebook(root)
notebook.pack(fill="both", expand=True)
for color in ("red", "orange", "green", "blue"):
frame = tk.Frame(notebook, background=color)
notebook.add(frame, text=color)
extra_frame = tk.Frame(notebook, background="white")
notebook.insert(2, extra_frame, text="white")
root.mainloop()
The first argument to insert
is a numerical index. Tabs are numbered starting with zero, so the position 0
will insert the tab as the first tab. You can use the string "end"
to insert the tab at the end.
last_frame = tk.Frame(notebook, background="black")
notebook.insert("end", last_frame, text="black")
The index must not be greater than the number of tabs.
Instead of a number, you can also specify a specific tab. For example, given the last_frame
example from above, a tab can be inserted immediately before it by using last_frame
instead of the integer position.
new_frame = tk.Frame(notebook, background='bisque')
notebook.insert(last_frame, new_frame, text="bisque")
Upvotes: 2