如何修复不工作的 Tkinter 嵌套框架;它不允许小部件

How to fix not-working Tkinter nested frame; it's not allowing widgets

我正在制作一个 D&D Character Randomizer,它已经完成,但我一直在尝试将我原来的单页 GUI 切换到一个更宽敞、更具包容性的多页 GUI。我制作了 5 个帧,我通过升高和降低来创建这种效果,效果很好,但是当我嵌套一个新框架并尝试将小部件绘制到该框架中时,它不起作用。

我在 Visual Studio 2017 年 运行 Anaconda 3.4。

import tkinter as tk
from cache import *
class interface(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title("D&D Character Randomizer")
        #c = tk.Canvas(self, height=boundary_height, width=boundary_width).grid()
        frames = (genPage, invPage, featPage, combatPage, spellPage)
        #all frames need to be initialized before the UI's are built
        self.frames = {}
        for F in frames:
            frame = F(self)
            frame.grid(row=0, column=0, sticky="nsew")
            self.frames[F] = frame
        for F in self.frames.values():
            F.build_UI()
        self.frames[genPage].tkraise()
    def topborder(self, master):
        genButton = tk.Button(master, text="General", width=boundary_width//wc, height=boundary_height//hc, bg=color_frame['gp'], fg="#ffffff", command=self.parent.frames[genPage].tkraise, font=button_font).grid(column=0, row=0, padx=button_pad_x, pady=button_pad_y)
        invButton = tk.Button(master, text="Inventory", width=boundary_width//wc, height=boundary_height//hc, bg=color_frame['ip'], fg="#ffffff", command=self.parent.frames[invPage].tkraise, font=button_font).grid(column=1, row=0, padx=button_pad_x, pady=button_pad_y)
        featButton = tk.Button(master, text="Features", width=boundary_width//wc, height=boundary_height//hc, bg=color_frame['fp'], fg="#ffffff", command=self.parent.frames[featPage].tkraise, font=button_font).grid(column=2, row=0, padx=button_pad_x, pady=button_pad_y)
        combatButton = tk.Button(master, text="Combat Stats", width=boundary_width//wc, height=boundary_height//hc, bg=color_frame['cp'], fg="#ffffff", command=self.parent.frames[combatPage].tkraise, font=button_font).grid(column=3, row=0, padx=button_pad_x, pady=button_pad_y)
        spellButton = tk.Button(master, text="Spells", width=boundary_width//wc, height=boundary_height//hc, bg=color_frame['sp'], fg="#ffffff", command=self.parent.frames[spellPage].tkraise, font=button_font).grid(column=4, row=0, padx=button_pad_x, pady=button_pad_y)
        generation = tk.Button(master, text="Generate", width=boundary_width//wc-4, height=boundary_height//hc, bg="#000000", fg="#ffffff", command=generate, font=button_font).grid(column=5, row=0, padx=button_pad_x)
#cross-class interface
class genPage(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent, bg=color_frame['gp'], height=boundary_height, width=boundary_width)
        self.parent=parent
        self.grid()
    def build_UI(self):
        #init topborder
        interface.topborder(self, self)
        self.render_frame_meta()
    def render_frame_meta(self):
        r,o=0,0
        m=tk.Frame(self, bg="#000000", height=100, width=100).grid(column=4, columnspan=2, sticky='ew')
        self.frame_meta_labels = []
        self.frame_meta_values = []
        for L in ['Race', 'Class', 'Background', 'Name', 'Level', 'Origin']:
            self.frame_meta_labels = tk.Label(m, text=L + ':', bg=color_frame['ip'], relief='ridge', font=metadata_font, anchor='w').grid()
            self.frame_meta_values = tk.Label(m, text='None', bg=color_frame['ip'], relief='ridge', font=metadata_font, anchor='e').grid()
            r=r+1
            if(r==3):
                r,o=0,1
    def refresh_UI(self):
        #c
        pass

我省略了其他页面,因为在我的开发过程中,它们是此时的占位符 - genPage 是我试图开始工作的内容。该框架实际上显示为一个黑色矩形(正如预期的那样),在我想要的位置(在 def render_frame 下称为 m)。

当我传递小部件时,for 循环中的标签会出现,但不会出现在框架中。他们实际上在做一些非常奇怪的事情——他们甚至没有出现在主框架上——他们出现在主框架下面。框架出现了,并且清晰可见,但是将小部件添加到框架只是将它们放在其他地方。

将行 m=to.Frame(… 更改为:

m = tk.Frame(…)
m.grid(…)

这应该可以解决您的问题。 请参阅此 link 了解原因:

Tkinter: AttributeError: NoneType object has no attribute