【问题标题】:Nested frames in tkintertkinter 中的嵌套帧
【发布时间】:2020-01-19 04:59:54
【问题描述】:

美好的一天,

使用 tkinter,Python 3.5 我正在尝试使用 for 循环和列表创建嵌套框架以生成框架名称。 这适用于框架内的第一级框架,但是下一级失败。

例如,此代码有效:

from tkinter import *
from tkinter import ttk

frameLevel1List = ['Frame1', 'Frame2', 'Frame3', 'Frame4']
frameLevel2List = ['FrameA', 'FrameB', 'FrameC', 'FrameD']

class myUI:
    def __init__(self):
        #create main window & frames
        self.main_window = Tk()
        self.main_window.title("frame2 UI V001 ")
        self.main_window.configure(background='gray')
        w=750
        h=500
        x=100
        y=100
        self.main_window.geometry("%dx%d+%d+%d" % (w, h, x, y))
        self.userlabel = Label(self.main_window, bg='gray', fg='white', text = "user Label")
        self.userlabel.pack(side="top")
        self.levellabel = Label(self.main_window, bg='gray', fg='white', text = "level Label")
        self.levellabel.pack(side="top")

        #create bottom frame
        bottomFrame = Frame(self.main_window, bg='white', height=500, width=800)
        bottomFrame.pack(side=BOTTOM)

        #create frames from first list
        for frame in frameLevel1List:
            self.frame=Frame(bottomFrame, width=800, height = 100, bg = 'green',  highlightbackground="black", highlightcolor="black", highlightthickness="1")
            self.frame.pack(side="top")
            self.framelabel = Label(self.frame, bg='blue', fg='white', text = frame)
            self.framelabel.place(x=10, y=10)


        mainloop()

UI=myUI()

但是,当我添加第二个 for 循环以在每个第一帧中添加第二个帧列表时,我收到错误消息。以下代码失败

from tkinter import *
from tkinter import ttk

frameLevel1List = ['Frame1', 'Frame2', 'Frame3', 'Frame4']
frameLevel2List = ['FrameA', 'FrameB', 'FrameC', 'FrameD']

class myUI:
    def __init__(self):
        #create main window & frames
        self.main_window = Tk()
        self.main_window.title("frame2 UI V001 ")
        self.main_window.configure(background='gray')
        w=750
        h=500
        x=100
        y=100
        self.main_window.geometry("%dx%d+%d+%d" % (w, h, x, y))
        self.userlabel = Label(self.main_window, bg='gray', fg='white', text = "user Label")
        self.userlabel.pack(side="top")
        self.levellabel = Label(self.main_window, bg='gray', fg='white', text = "level Label")
        self.levellabel.pack(side="top")

        #create bottom frame
        bottomFrame = Frame(self.main_window, bg='white', height=500, width=800)
        bottomFrame.pack(side=BOTTOM)

        #create frames from first list
        for frame in frameLevel1List:
            self.frame=Frame(bottomFrame, width=800, height = 100, bg = 'green',  highlightbackground="black", highlightcolor="black", highlightthickness="1")
            self.frame.pack(side="top")
            self.framelabel = Label(self.frame, bg='blue', fg='white', text = frame)
            self.framelabel.place(x=10, y=10)

            #create frames from second list
            for frame2 in frameLevel2List:
                self.frame2=Frame(frame, width=800, height = 50, bg = 'yellow')
                self.frame2.pack(side="top")
                self.frame2label = Label(self.frame2, bg='blue', fg='white', text = frame2)
                self.frame2label.place(x=10, y=10)

        mainloop()

UI=myUI()

这是错误信息:

Traceback (most recent call last):
  File "C:\Users\Nicholas Boughen\Desktop\py\rubrics\nestedFramesTest.py", line 43, in <module>
    UI=myUI()
  File "C:\Users\Nicholas Boughen\Desktop\py\rubrics\nestedFramesTest.py", line 36, in __init__
    self.frame2=Frame(frame, width=800, height = 50, bg = 'yellow')
  File "C:\Users\Nicholas Boughen\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2584, in __init__
    Widget.__init__(self, master, 'frame', cnf, {}, extra)
  File "C:\Users\Nicholas Boughen\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2132, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Users\Nicholas Boughen\AppData\Local\Programs\Python\Python35\lib\tkinter\__init__.py", line 2110, in _setup
    self.tk = master.tk
AttributeError: 'str' object has no attribute 'tk'

任何可以帮助我理解我做错了什么并使用 for 循环创建嵌套框架的内容将不胜感激。

也许有一种完全不同的方式来创建嵌套框架会更好?我要做的是从列表中生成框架名称并随着列表的变化而改变界面。因此,如果列表中的项目更多或更少,界面中的框架就会更多或更少。该列表将从不同的界面进行编辑。

【问题讨论】:

  • 完整的错误信息是什么? self.course 应该是什么?请注意,self.frameself.frame2 等作为实例属性毫无意义——它们将被您的循环多次覆盖,并且最终只会保留最新的小部件。它们也可能是局部变量。
  • edit您的问题包含完整的错误。
  • 谢谢。我已编辑以包含错误并修复了错误的“课程”。我是编码新手,这是我关于堆栈溢出的第一个问题,感谢您的指导。

标签: list for-loop tkinter nested frames


【解决方案1】:

在研究其他有类似问题的人以及@jasonharper 评论后,我发现我需要在创建框架 ID 时将其保存(附加)到列表中,以确保每个小部件保持唯一。

这是我想做的代码:

from tkinter import *
import functools

class practice:
    def __init__(self,root):
        self.frame_list = []


        for w in range(5):
            frame = Frame(root, width=800, height = 100, bg = 'green',  highlightbackground="black", highlightcolor="black", highlightthickness="1")
            frame.pack(side="top")
            self.frame_list.append(frame)

            for w in range(5):
                frame = Frame(root, width=400, height = 50, bg = 'blue',  highlightbackground="black", highlightcolor="black", highlightthickness="1")
                frame.pack(side="top")
                self.frame_list.append(frame)

                for w in range(5):
                    frame = Frame(root, width=200, height = 25, bg = 'yellow',  highlightbackground="black", highlightcolor="black", highlightthickness="1")
                    frame.pack(side="top")
                    self.frame_list.append(frame)


        print('button list is', self.frame_list)



root = Tk()
root.title("frame2 UI V001 ")
root.configure(background='gray')
w=750
h=500
x=100
y=100
root.geometry("%dx%d+%d+%d" % (w, h, x, y))

谢谢,我以后会尝试更清楚地发布问题。

【讨论】:

    猜你喜欢
    • 2014-02-07
    • 1970-01-01
    • 2013-06-19
    • 2017-08-31
    • 2021-07-09
    • 1970-01-01
    • 2019-11-23
    • 2020-09-14
    • 1970-01-01
    相关资源
    最近更新 更多