0

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?

SuperKogito
  • 2,998
  • 3
  • 16
  • 37
Medjed
  • 9
  • 3
  • 1
    While it's possible, I think this might be a classic [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). How are you going to write code that uses (references) the generated names if you don't know what they are in advance? I suggest using a list or dictionary container and store the widgets in it and reference them either through their index or associated key. – martineau Apr 05 '19 at 22:39
  • Well my original thought was like this. Normally i instantiate a frame by saying for example frame1 = tk.Frame(). So when iterating through the list of macro objects i was thinking i could have like a combination of frame and object.name that way i could append frameMedjed to a list for example and iterate through that to do configurations to the frame. But yeah.. I guess I might need to do something like you suggested. – Medjed Apr 05 '19 at 22:42
  • 1
    See [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables). Also note that the keys of a dictionary don't have to be strings so there's a lot of flexibility. – martineau Apr 05 '19 at 22:45
  • 1
    Thanks martineau, I think I will try to solve it with dictionaries the value i was going to use for the actual tab labels could be the keys for example. :) – Medjed Apr 05 '19 at 22:53
  • Note that tkinter widgets all have a `winfo_name()` method that returns a unique value that you should be able to use as a dictionary key. Here's a little [documentation](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html) about it. You can also add custom attributes to any widget (including `Frame`s) to associate additional information with one (just don't use the name of an standard attributes they might have). – martineau Apr 05 '19 at 22:58
  • @Medjed: Looks like a treeveiw, read about [Treeview widget](https://tkdocs.com/tutorial/tree.html) – stovfl Apr 06 '19 at 09:27

1 Answers1

0

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.stack.imgur.com/YPDmz.png

Medjed
  • 9
  • 3