Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有一种迭代的方法来创建tk.Frames并对其进行配置?_Python_Python 2.7_Tkinter - Fatal编程技术网

Python 有没有一种迭代的方法来创建tk.Frames并对其进行配置?

Python 有没有一种迭代的方法来创建tk.Frames并对其进行配置?,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,现在我有: # create window frames self.f1 = tk.Frame(self.root) self.f2 = tk.Frame(self.root) self.f3 = tk.Frame(self.root) self.f4 = tk.Frame(self.root) self.f5 = tk.Frame(self.root) self.f6 = tk.Frame(self.root) # place frames o

现在我有:

# create window frames
    self.f1 = tk.Frame(self.root)
    self.f2 = tk.Frame(self.root)
    self.f3 = tk.Frame(self.root)
    self.f4 = tk.Frame(self.root)
    self.f5 = tk.Frame(self.root)
    self.f6 = tk.Frame(self.root)

# place frames on window
    for f in (self.f1, self.f2, self.f3, self.f4, self.f5, self.f6):
        f.configure(bg="white")
        f.configure(width=self.width, height=self.height, bg="white")
        f.place(x=0, y=0)

我将添加更多的帧。我想知道是否有一种迭代的方法来创建所有帧,并将它们放在窗口中进行配置,而不必键入“self.f7、self.f8、self.f9”等。

将每个新的
帧添加到列表中,然后在列表上迭代

frames = []

self.f1 = tk.Frame(self.root)
frames.append(self.f1)
# Do that for all frames

for f in frames:
    f.configure(bg="white")
    f.configure(width=self.width, height=self.height, bg="white")
    f.place(x=0, y=0)
编辑以回答评论:

为此创建一个方法:

def add_frames(self, how_many_frames):
    for i in range(how_many_frames):
        f = tk.Frame(self.root)
        self.frames[i] = f
        f.configure(bg="white")
        f.configure(width=self.width, height=self.height, bg="white")
        f.place(x=0, y=0)

您还需要在
\uuu init\uu
方法中初始化
self.frames=dict()。现在调用
add_frames(30)
创建30个帧,然后将其存储在字典中的
self.frames
下,并同时对其进行配置。

但是在附加每个“Frame”对象之前,我是否还需要声明它们?有没有办法输入一个整数,比如说30,然后让它自动在列表中填充self.f1、self.f2、self.f3?这就是你要找的吗?差不多!我将如何使用约定f1、f2、f3等命名它们?我是否将这些名称作为键存储在字典中?(或者换句话说,如何单独访问它们?我是否必须使用“self.frames[1]、“self.frames[2]、“self.frames[3]”等?正如您在我的示例中所看到的那样,
self.frames[I]=f
,帧存储在字典中,其中的键只是一个数字(在这种情况下,数字从0到29,因为我创建了30个帧)。你可以有类似于
self.frames[“f”+str(i+1)]=f
的键,比如
f1、f2
等等。啊,我明白了。谢谢!我正想弄明白同样的事情,但我忘了“str()”