I'm trying to create new tabs in my navigation bar notebook by clicking on the last tab. To further complicate the task my application is written with classes. My current and less elegant solution requires an Entry with a Button to create the new tab with the title entered in the Entry widget. Does anyone know a more elegant solution to my problem?
Heres my code:
import tkinter as tk
from tkinter import ttk
class MainApp(tk.Tk):
"""Main window class"""
def __init__(self):
super(MainApp, self).__init__()
self.geometry("1000x1000")
self.main_window = tk.Frame(self)
self.main_window.pack(side="top", fill="both", expand=True)
# saving all tabs & Frames to a dictionary to be able to access them later
self.frames = {}
self.tabs = {}
# create a tab bar
self.navbar = Navbar(self.main_window)
# set the layout
self.set_layout()
def set_layout(self):
"""creates the default app layout"""
self.add_tab("Settings") # first page
self.add_tab("Case 1")
def add_tab(self, title):
"""adds a new tab with name title to the navbar"""
tab = ttk.Frame(self)
self.navbar.add(tab, text=title)
self.tabs[title] = tab
# check if Settings or Case type Tab, to create MainInput frame with correct buttons
if title.lower().find("setting") != -1:
self.frames[title] = MainInput(self, tab, self.navbar, settings=True)
else:
self.frames[title] = MainInput(self, tab, self.navbar, case=True)
self.navbar.pack(fill=tk.BOTH, expand=tk.YES)
for tab in self.tabs:
self.frames[tab].grid(sticky="nesw")
def add_frame(self, title, frame):
"""adds the frame to frames dict with key title"""
self.frames[title] = frame
class Navbar(ttk.Notebook):
"""returns a Notebook"""
def __init__(self, parent):
ttk.Notebook.__init__(self, parent)
@staticmethod
def delete(tab):
"""delete current tab"""
tab.forget(tab.select())
class MainInput(tk.Frame):
"""The base frame of every tab"""
def __init__(self, root, parent, notebook, settings=False, case=False):
tk.Frame.__init__(self, parent)
# Either build a settings or testcase tab
if settings is True:
SettingsField(root, parent)
if case is True:
CaseGeneral(parent, notebook)
class SettingsField(tk.Frame):
"""Creates a settings tab"""
def __init__(self, root, parent):
tk.Frame.__init__(self, parent)
# add the "new tab" name entry and button
tk.Label(parent, text="Add new Testcase:").grid(row=0, column=0, columnspan=2, sticky="w")
tk.Label(parent, text="Name:").grid(row=1, column=0, sticky="w", padx=20)
new_tab_title = tk.Entry(parent, textvariable=tk.StringVar(), width=30)
new_tab_title.grid(row=1, column=1, columnspan=2, sticky="w")
add_button = tk.Button(parent, text="add", command=lambda: [root.add_tab(new_tab_title.get())])
add_button.grid(row=1, column=3, sticky="w", padx=5)
class CaseGeneral(tk.Frame):
def __init__(self, parent, notebook):
tk.Frame.__init__(self, parent)
# create "delete current tab" buton
tk.Label(parent, text="delete Testcase").grid(row=0, column=0)
delete_button = tk.Button(parent, text="delete", command=lambda: [Navbar.delete(notebook)])
delete_button.grid(row=0, column=1, sticky="w")
Images of the running code: Settings Tab Cases
Note: Please don't roast me since this is my first post here :)