Search code examples
pythonuser-interfacetkinteranacondaframe

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


I'm making a D&D Character Randomizer, which is completed, but I have been trying to switch my original, single-page GUI to a more spacious and inclusive multi-page GUI. I make 5 frames that I raise and drop to create this effect, which works well, but when I nest a new frame, and try to draw widgets into this frame, it doesn't work.

I'm running Anaconda 3.4 on Visual Studio 2017.

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

I've left out the other pages because they're placeholders at this point in my development - genPage is what I'm trying to get to work. The frame actually appears as a black rectangle (as expected) where I want it to (called m underneath def render_frame).

When I pass my widgets, the labels in the for loop, they appear, but not in the frame. They're actually doing something really weird - they don't even appear on the main frame - they appear below it. The frame appears, and it is clearly visible, but adding widgets to the frame just puts them somewhere else.


Solution

  • Change the line m=to.Frame(… to:

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

    That should fix your problem. See this link to understand why:

    Tkinter: AttributeError: NoneType object has no attribute