I found a very elegant version of tab creation here : Tkinter Notebook create new tabs by clicking on a plus/+ tab like every web browser
Unfortunately, as I am trying this solution, I get an 'IndexError: tuple index out of range'
Don't make fun of me please, it's my first post here, and I just started learned Python a few months ago :)
This script is supposed to create a new tab based on a custom frame each time I click on the "+" tab, but the app window doesn't appear, and instead I get this "IndexError"
import tkinter as tk
from tkinter import ttk
class MainWindow(tk.Tk):
def __init__(self, **kwargs):
super().__init__(**kwargs)
notebook = ttk.Notebook(self)
notebook.bind("<<NotebookTabChanged>>", self.add_tab(notebook))
notebook.grid(sticky="NSEW")
frame = ttk.Frame(self)
notebook.add(frame, text="+")
def add_tab(event, notebook):
if notebook.select() == notebook.tabs()[-1]:
index = len(notebook.tabs())-1
frame = CustomFrame(notebook)
notebook.insert(index, frame, text=f"Frame {index +1}")
notebook.select(index)
class CustomFrame(ttk.Frame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
root = MainWindow()
root.mainloop()