How can I create multiple frames with a for loop on a Tkinter window?

Viewed 127

Note I tried to put this in stack overflow with correct formatting, but even when I pressed the code formatting button, it didn't process the response. Can someone help me with that as well?. I am posting here without the formatting as I need help quite fast. I know many people on this forum are really good at programming, so I thought of reaching out.

I am working on an application using python, and more specifically, I'm using Tkinter for the user interface. I have over twenty new pages I need to make within the application, and I as a result, I thought of using a for loop and an outline class structure of which I would create new instances (as their own pages which would later be linked around the application using buttons) to be the classes I use. However, I continue to get the following error when I run my code:

File "setup.py", line 215, in <module>
pages_dict[info_req[info][filetype][0]] = outline_info(new_object)
TypeError: __init__() takes exactly 4 arguments (2 given)

I understand why this makes sense, as the init function definition contains 4 arguments, but I am not sure about how I can make the controller (which is defined in the initial application class) and the parent window part of the arguments for the instance of the outline_info class, as I cannot refer to self in those scenarios as the classes haven't even been declared or made up until that point of declaration as an instance (If this explanation seems confusing, please look at the code below for further clarification as well).

An excerpt of my code is shown below, addressing the above concerns. Please let me know if more information is needed to understand or clarify my problem. The code below does not include many other classes I have defined, as well as an array called info_req, which contains a database of information.

class Application(tk.Tk):
        def __init__(self, *args, **kwargs):
            tk.Tk.__init__(self, *args, **kwargs)
    
            # the container is where we'll stack a bunch of frames
            # on top of each other, then the one we want visible
            # will be raised above the others
            container = tk.Frame(self)
            container.pack(side="top", fill="both", expand=True)
            container.grid_rowconfigure(0, weight=1)
            container.grid_columnconfigure(0, weight=1)
    
    
            self.frames = {}
            for F in (PageOne, PageThree, PageFour, indpage, familypage, FinalPage):
                page_name = F.__name__
                frame = F(parent=container, controller=self)
                self.frames[page_name] = frame
    
                # put all of the pages in the same location;
                # the one on the top of the stacking order
                # will be the one that is visible.
                frame.grid(row=0, column=0, sticky="nsew")
    
            self.show_frame("PageOne")
    
        def show_frame(self, page_name):
            '''Show a frame for the given page name'''
            frame = self.frames[page_name]
            frame.tkraise()
    
    class outline_info(tk.Frame):
    
        def __init__(self, parent, controller, info_type_array):
            
            count = 0 # Just for reference
            tk.Frame.__init__(self, parent)
            self.controller
            first_title = Label(self, text=info_type_array[0]).grid(row=1,column=1)
            for i in range(1, len(info_type_array)):
                text_first = Label(self, text=str(info_type_array[i])).grid(row=d+1, column=1)
                entry_first = Entry(self, width=20).grid(row=d+1, column=2)
                count += 1
            def submit_again():
                controller.show_frame("indpage")
                return "hello" # Do I still need this?
            submit2 = Button(self, text="Submit Details", bg="blue", command=submit_again)
            submit2.pack(side=BOTTOM)
    pages_dict = {}
    for i in range(0, len(info_req)):
        for filetype in range(0, len(info_req[i])):
            if filetype!=0:
                new_object = info_req[i][filetype]
                if info_req[i][filetype][0] not in pages_dict.keys():
                    pages_dict[info_req[i][filetype][0]] =outline_info(new_object)

Many thanks.

Edit:

The following is a snippet of info_req. I am creating a beginner's travel guide, but I really want to learn how to tackle the problem in the original post.

info_req = [ [ ["Places Visited"], ["Country 1", "City Visited", "Reccomendations"], ["Country 2", "City Visited", "Reccomendations"] ], [ ["Favorite Food"], ["Food 1", "Cuisine", "Taste", "Flavor"], ["Food 2", "Cuisine", "Taste", "Flavor"] ], [ ["Favorite Airlines"], ["Airline 1", "Organization", "Position", "Duration"] ] ]

0 Answers
Related