首页 > 解决方案 > 如何在 Tkinter 窗口上使用 for 循环创建多个帧?

问题描述

注意我试图用正确的格式把它放在堆栈溢出中,但是即使我按下代码格式化按钮,它也没有处理响应。有人可以帮助我吗?我在这里发帖时没有格式化,因为我需要非常快的帮助。我知道这个论坛上很多人都非常擅长编程,所以我想到了伸出援手。

我正在使用 python 开发一个应用程序,更具体地说,我使用 Tkinter 作为用户界面。我需要在应用程序中创建超过 20 个新页面,因此,我想到了使用 for 循环和大纲类结构,我将在其中创建新实例(作为它们自己的页面,稍后将链接到使用按钮的应用程序)成为我使用的类。但是,当我运行我的代码时,我继续收到以下错误:

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)

我理解为什么这是有道理的,因为 init 函数定义包含 4 个参数,但我不确定如何制作控制器(在初始应用程序类中定义)和实例的参数的父窗口部分outline_info 类,因为在这些情况下我不能引用 self ,因为在声明点作为实例之前,这些类甚至还没有被声明或组成(如果这种解释看起来令人困惑,请查看下面的代码以进一步澄清出色地)。

我的代码摘录如下所示,解决了上述问题。如果需要更多信息来理解或澄清我的问题,请告诉我。下面的代码不包括我定义的许多其他类,以及一个名为 info_req 的数组,其中包含一个信息数据库。

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)

非常感谢。

编辑:

以下是 info_req 的片段。我正在创建一个初学者的旅行指南,但我真的很想学习如何解决原始帖子中的问题。

info_req = [ [ [“去过的地方”],[“国家 1”,“去过的城市”,“推荐”],[“国家 2”,“去过的城市”,“推荐”] ],[ [“最喜欢的食物” ]、[“食物 1”、“美食”、“味道”、“风味”]、[“食物 2”、“美食”、“味道”、“风味”] ]、[[“最喜欢的航空公司”]、[ “航空公司 1”、“组织”、“位置”、“持续时间”] ] ]

标签: pythonclassoopobjecttkinter

解决方案


推荐阅读