【问题标题】:How to create multiple frames in python through For Loop?如何通过 For Loop 在 python 中创建多个帧?
【发布时间】:2021-04-29 04:40:38
【问题描述】:

我正在使用 Tkinter 进行 Python 的 GUI 编程。
我正在使用以下代码在我的 Tkinter 根窗口中创建 4 个帧(Frame1、Frame2、Frame3 和 Frame4):

import tkinter as tki

class App(object):

    def __init__(self):
        self.root = tki.Tk()        
        self.root.wm_title("Play With python")
        for r in range(8):
            self.root.rowconfigure(r, weight=1)
        for c in range(2):
            self.root.columnconfigure(c, weight=1)
    # Create Frames
        Frame1 = tki.Frame(self.root, borderwidth=1, bg = 'blue')
        Frame1.grid(row = 0, column = 0, rowspan = 2, columnspan = 2, sticky = 'w, e, n, s')

        Frame2 = tki.Frame(self.root, borderwidth=1, bg = 'blue')
        Frame2.grid(row = 2, column = 0, rowspan = 2, columnspan = 2, sticky = 'w, e, n, s')

        Frame3 = tki.Frame(self.root, borderwidth=1, bg = 'blue')
        Frame3.grid(row = 4, column = 0, rowspan = 2, columnspan = 2, sticky = 'w, e, n, s')

        Frame4 = tki.Frame(self.root, borderwidth=1, bg = 'blue')
        Frame4.grid(row = 6, column = 0, rowspan = 2, columnspan = 2, sticky = 'w, e, n, s')

app = App()
app.root.mainloop()

我想使用 for 循环 使我的代码可读,因为在我的真实代码中帧大约为 12。

我正在尝试以下代码以获得相同的输出:

import tkinter as tki

class App(object):

    def __init__(self):
        self.root = tki.Tk()        
        self.root.wm_title("Play With python")
        for r in range(8):
            self.root.rowconfigure(r, weight=1)
        for c in range(2):
            self.root.columnconfigure(c, weight=1)
    # Create Frames
        j = 0
        for i in range(1,5):
            Framei = tki.Frame(self.root, borderwidth=1, bg = 'blue')
            Framei.grid(row = j, column = 0, rowspan = 2, columnspan = 2, sticky = 'w, e, n, s') 
            j = j + 2
app = App()
app.root.mainloop()

问题:

正如我看到的问题,这段代码创建了 4 个帧,但与 Framei 具有相同的名称引用(而不是 Frame1、Frame2、Frame3 和 Frame4)。 但我想要 4 个不同名称的独立框架(Frame1、Frame2、Frame3 和 Frame4)。

我知道我的问题与在运行时创建变量非常相关,但由于我是第一次应用它所以面临问题。

我使用的 Python 版本是 3.0.1。
请帮忙...

【问题讨论】:

  • 您应该将答案标记为正确(单击对勾)。

标签: python


【解决方案1】:

您应该将Frame 实例放在一个列表中,如下所示:

import tkinter as tki

class App(object):

    def __init__(self):
        self.root = tki.Tk()
        self.root.wm_title("Play With python")
        for r in range(8):
            self.root.rowconfigure(r, weight=1)
        for c in range(2):
            self.root.columnconfigure(c, weight=1)

        # Create a list of the Frames in the order they were created
        frames = []
        j = 0
        for i in range(1,5):
            Frame = tki.Frame(self.root, borderwidth=1, bg = 'blue')
            Frame.grid(row = j, column = 0, rowspan = 2, columnspan = 2, sticky = 'w, e, n, s')

            # Add the Frame to the list
            frames.append(Frame)

            # Also, just as an FYI, j = j + 2 can be better written like this
            j += 2

        # To demonstrate
        print(frames)

        # This is the first Frame created
        print(frames[0])

app = App()
app.root.mainloop()

要访问框架,只需索引列表即可。

【讨论】:

  • 感谢 iCodez 的回答。我的问题解决了,概念也很清楚了。如果我们在其他编程语言(比如 C)中做同样的事情,那么我们将在那里使用数组的概念。谢谢
  • @AshA:很高兴我能帮上忙。但是,如果您认为我的回答适当地解决了您的问题,那么您应该接受它以保持整洁有序。如果没有,我很乐意提供进一步的帮助。
  • 是的,您的回答解决了我的问题,我的工作完成了 90%。你能推荐一些阅读材料来探索更多关于 Python 中的 GUI 编程吗??
  • @Asha:是的,我想我有一些好东西:tkdocs.com/tutorial/onepage.html effbot.org/tkinterbook 这些是我自己用来解决问题的(当然除了 SO)。
  • 感谢 ICodez,链接真的很有帮助,因为我对阅读“docs.python.org”库感到困惑,但我并没有从那里获得更多与 GUI 相关的信息。谢谢
猜你喜欢
  • 2015-05-03
  • 2013-10-13
  • 1970-01-01
  • 2017-03-20
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 1970-01-01
  • 2014-03-02
相关资源
最近更新 更多