如何在 Tkinter 窗口上使用 for 循环创建多个框架?



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

我正在使用python开发一个应用程序,更具体地说,我正在使用Tkinter作为用户界面。我需要在应用程序中制作二十多个新页面,因此,我想使用一个 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

", "组织", "职位", "持续时间"] ]

解决方案

由于您希望控制器可访问,因此可以使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。请注意,在我发布的代码中,我没有发布工作示例,但是 soultion 是如何工作的,因为源代码包含的代码比您发布的要多得多,因此流程不清楚。如果你想在Application类中使用任何东西,只需self使用它,因为self实际上是传递给Application类的submit_info对象。希望这有帮助!

最新更新