Oddly enough, I have continued experimenting on this, and expanded on the earlier work by replacing the ad-hoc data structure used previousl.y with a set of classes (including two types of exceptions) representing the data explicitly. There is a small test program at the end of the file to show how it can be used.
from tkinter import *
import tkinter as tk
from tkinter import ttk, Label
class TabSet(ttk.Notebook):
def __init__(self, root, parent):
super().__init__(root)
self.root = root
self.parent = parent
self.tabEntries = dict()
def add(self, title, **kwargs):
if title not in self.tabEntries.keys():
self.tabEntries[title] = TabEntry(title)
if "tab" in kwargs.keys():
if self.tabEntries[title].tab != None:
raise TabSetDuplicateTabException(title)
else:
self.tabEntries[title].tab = kwargs["tab"]
super().add(self.tabEntries[title].tab, text=title)
if "subentry" in kwargs.keys():
self.tabEntries[title].add(kwargs["subentry"])
if "button_name" in kwargs.keys() and "json_request" in kwargs.keys() and "expected_response" in kwargs.keys():
self.tabEntries[title][kwargs["button_name"]] = TabSubentry(kwargs["button_name"], kwargs["json_request"], kwargs["expected_response"])
class TabEntry:
def __init__(self, tab_title):
self.tab_title = tab_title
self.tab = None
self.subentries = dict()
def add(self, subentry):
if subentry.button_name not in self.subentries.keys():
self.subentries[subentry.button_name] = subentry
else:
raise TabEntryDuplicateSubentryException(self.tab_title, subentry.button_name)
class TabSubentry:
def __init__(self, button_name, json_request, expected_response):
self.button_name = button_name
self.json_request = json_request
self.expected_response = expected_response
class TabSetDuplicateTabException(Exception):
def __init__(self, title):
self.title = title
class TabEntryDuplicateSubentryException(Exception):
def __init__(self, title, button):
self.title = title
self.button = button
if __name__ == "__main__":
root = tk.Tk()
root.title("Tab Widget")
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w, h))
frame = Label(root, text='')
frame.pack(side='top', fill='x', expand=False)
tabControl = TabSet(root, frame)
tabControl.add("Tab 1",
tab = ttk.Frame(tabControl),
button="Button 1", json_request="{}", expected_response="")
ttk.Label(tabControl.tabEntries["Tab 1"].tab, text="Button 1")
tabControl.add("Tab 2", subentry=TabSubentry("Button A", "{foo}", "foo"))
ttk.Label(tabControl.tabEntries["Tab 2"].tab, text="Button …