Thomas Carroll
Thomas Carroll

Reputation: 161

Toplevel displaying contents in previous frame

I have a Toplevel class DesignWindow which hold a 10x10 array of buttons, when I call it in the way discussed here: Tkinter; Toplevel in a new class it creates the window within the last.

example

What have I done wrong? The only difference is that my 'root' window (with the menu) is a tk.Frame instead of the tk.Tk in the quoted question.

class MainWindow(tk.Frame):
    """Draw the main window"""
    def __init__(self, parent):
        tk.Frame.__init__(self, parent, background='grey')
        self.parent = parent
        self.menuscreen = MenuScreen(self)

        self.grid()

        self.menuscreen.design.grid(column=0, row=0)

class MenuScreen(tk.Frame):
    """Create the menu screen"""
    def __init__(self, parent):
        self.design = tk.Button(command=self.create_design_window, text="Design")

    def create_design_window(self):
        self.design_window = DesignWindow(self)

class DesignWindow(tk.Toplevel):
    """Frame for design mode"""
    def __init__(self, main):
        tk.Toplevel.__init__(self)

EDIT Generation of the Buttons:

     self.btn = [[0 for x in range(10)] for x in range(10)]
        for column in range(10):
            for row in range(10):
                self.btn[column][row] = tk.Button(main)
                self.btn[column][row].grid(column=column, row=row)

Upvotes: 0

Views: 136

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385890

I'm not sure there's enough code in your answer to know for certain, but there are definitely two bugs that I see right away.

First, MenuScreen isn't calling the __init__ of it's superclass. You need to add this to MenuScreen.__init__:

tk.Frame.__init__(self, parent)

Second, you aren't giving a parent to the widgets in MenuScreen, so they are added to the root window. You should get in the habit of always providing a parent:

self.design = tk.Button(self, ...)

You don't show how you create any widgets in DesignWindow, but I'm guessing you're making the same mistake. You need to make sure that all widgets have an explicit parent or they will end up in the root window.

Upvotes: 1

Related Questions