python - 如何在 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 个参数,但我不确定如何使 Controller (在初始应用程序类中定义)和参数的父窗口部分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", "菜系", "味道", " flavor "], ["食物 2", "菜系", "味道", " flavor "] ], [ ["最喜欢的航空公司"], ["航空公司1", "组织", "职位", "时长"] ] ]

最佳答案

解决方案

如果您希望 Controller 可访问,您可以让outline_info 类继承Application 类。然后,在 outline_info 类中调用 super().__init__() 并且不要实例化 Application 类,而是实例化 outline_info类。这是您的代码:

代码

class Application(tk.Tk):
        def __init__(self, *args, **kwargs):
            super().__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(Application):
         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)

有一些格式错误,希望您能修复!到处使用相同的缩进(有些地方 8 个空格,有些地方 4 个空格,您已经使用过)。

建议和其他信息

你在某些地方使用了tk.wdg,在某些情况下直接使用了wdg。请注意,在我发布的代码中,我没有发布一个工作示例,但由于源代码包含的代码比您发布的代码多得多,因此流程不清楚,所以灵魂是如何工作的。如果你想在 Application 类中使用任何东西,只需将它与 self 一起使用,因为 self 实际上是传递给 submit_info 的对象 应用程序类。希望这对您有所帮助!

https://stackoverflow.com/questions/62637033/

相关文章:

c - 尝试在 bmp 文件中画线时如何在 C 中实现 Bresenham 的线算法?

swift - 达到最大时间戳计数,不会记录更多事件 Xcode

flutter - 如何解决 PlatformException (PlatformExceptio

r - 控制 geom_line() 图表中的日期(x 轴)间隔

asp.net - 如何为网络浏览器的用户获取 AD 属性

reactjs - 如何使用 react-email-editor 加载 HTML 模板而不是 js

amazon-web-services - server.servlet.context-path

google-chrome - 报告 api : Is there a way to debug w

c# - EF 核心错误 : "Comparison on entity type is not s

python - 在不阻塞其他任务的情况下限制 celery 任务的速率