Search code examples
pythontkinterttk

Possible to create varying number of frame/tab objects and bind each to a name?


I am reading a folder structure for a game where each account has subfolders for servers and then subfolders for characters and in the character folders there is a text file.

What i am trying to create with tkinter and ttk in my root window is 1tab / frame for each account and under each account 1 tab for each server and then 1 tab for each character where i will finaly put text from each characters file.

Since i read this from the file system the amount of accounts, servers and characters can vary so i am looping through these to make the tkinter widget objects.

Problem is i can't figure out how to bind each object to a name so i can make comparisons / modify values of the objects after they are created, in order to just have 1 tab per server for example where all characters are put.

I have tried googling but can't seem to find what i am looking for. So far i have tried creating a class for each file i will read and the class contains all necessary info. But i get stuck when creating the tkinter objects and trying to figure out how to go about it.

This is my class from which i am successfully creating an instance with all info populated for each file, although they are in a list of objects with no bound name/variable:

class MacroObject(object):
    def __init__(self, name, realm, account, path):
        self.account = account
        self.realm = realm
        self.name = name
        self.path = path
        self.macroName = ''
        self.macroText = []

And this is my current approach to create the gui:

root = tk.Tk()

tabControl = ttk.Notebook(root)
tabControl.pack()

account_tabs = []
realm_tabs = []
character_tabs = []

accounts = []
realms = []

for objects in macro_objects:
    if objects.account not in accounts:
        tabControl.add(ttk.Frame(tabControl), text=objects.account)
        accounts.append(objects.account)
        for objects in macro_objects:
            if objects.account == ttk.Frame.text and objects.realm not 

This is where i get stuck and the last line obviously wouldn't work, the ttk.Frame is instantiated with seemingly no way for me to refer to it and i can't seem to figure out how to solve it.

I am still very much a beginner so I'm guessing this might be a stupid question and there is probably a much simpler approach for this?


Solution

  • My problem was solved thanks to Martineau reminding/making me realize i could store the widgets in a dictionary.

    I used values from my MacroObject class instances as keys and after a while i got the result i wanted with a ttk notebook tab hierarchy: https://i.sstatic.net/YPDmz.png