0

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)
Thomas Carroll
  • 161
  • 1
  • 13
  • Please read [How to create a Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) – Bryan Oakley Dec 09 '16 at 20:15
  • Does the `MenuScreen.__init__` not need to call it's super and don't you need to pass the parent to both super constructors. – Paul Rooney Dec 09 '16 at 20:15
  • Really sorry, forgot the add the top class that actually does the rendering bit :/ – Thomas Carroll Dec 09 '16 at 20:44
  • Are you asking why widgets that should be in the toplevel are instead in another window? If so, you need to show us how you're creating the windows in the toplevel. – Bryan Oakley Dec 09 '16 at 21:09
  • This sounds like what I'm asking. As for how I'm creating each widget it is an instance of tk.Button() I'm not sure how you 'create a window' in the Toplevel and have seen no mention of such. Perhaps you could link a resource which explains this? – Thomas Carroll Dec 09 '16 at 21:11

1 Answers1

1

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I am following this: https://stackoverflow.com/questions/17466561/best-way-to-structure-a-tkinter-application as a way to structure so it makes sense not to right? – Thomas Carroll Dec 09 '16 at 21:13
  • 1
    @ThomasCarroll: _"so it makes sense not to"_ -- makes sense not to _what_? You must call the constructor of the superclass, and you must explicitly give every widget a parent. – Bryan Oakley Dec 09 '16 at 21:14
  • I'm feeling as if I have a fundamental misunderstanding of what is happening here? I have followed your advice but it gives a window with no buttons on account of the widgets not being loaded as before. I am going to try restructuring my program and see if it makes any changes. – Thomas Carroll Dec 09 '16 at 21:24
  • 1
    @ThomasCarroll: there's nothing wrong with the structure, you're just failing to use tkinter properly. If you subclass a widget, you must call the `__init__` of the widget you are inheriting from. When you create a widget, you must tell it what window is its parent. If you don't tell every widget what its parent is, it goes to the root window. If you would give a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) we could perhaps be more helpful. – Bryan Oakley Dec 09 '16 at 21:26
  • Okay I have edited my post so you can see how the buttons are made, is main the parent here? It seems I haven't defined a 'parent' in the Toplevel. Further, when I run the program now no widgets are displayed at all! – Thomas Carroll Dec 09 '16 at 21:36
  • Right, I've gone through and given it a good thought. This was mostly a problem with the parent classes for each button, as you said, thanks A LOT for your help! – Thomas Carroll Dec 09 '16 at 21:58