Question

I don't know why this is happening but I'm trying to create an "Options" child window with Tkinter from a MenuBar. The child window pops up but when I try to create a label within the child window, the label appears on the main window... I don't know why this is happening. I have been searching the internet for a while and can't find an answer to my problem. Here is the code.

class slot(Frame):
def __init__(self):
    self.root = Frame.__init__(self)
    # Set up the main window and the variables 
    self.master.title("Slot Machine")
    # Open in full screen
    self.w, self.h = self.master.winfo_screenwidth(), self.master.winfo_screenheight()
    self.master.geometry("%dx%d+0+0" % (self.w, self.h))
    # Add the drop down menu
    menubar = Menu(self.master)
    self.master.config(menu=menubar)
    fileMenu = Menu(menubar)
    fileMenu.add_command(
        label="New Game", 
        command=self.__init__,
        underline = 0
    )
    fileMenu.add_command(
        label="Options", 
        command=self.newStartingValue,
        underline = 0
    )
    fileMenu.add_command(
        label="Exit", 
        command=self.quit,
        underline = 0
    )
    fileMenu.add_separator()
    menubar.add_cascade(
        label = "File", 
        menu = fileMenu, 
        underline = 0
    )
    helpMenu = Menu(menubar)
    helpMenu.add_command(
        label="About...", 
        command=self.showHelp,
        underline = 0
    )
    menubar.add_cascade(label="Help", menu = helpMenu, underline = 0)
    # Manage the main window and center everything
    self.grid(sticky = W+E+N+S)
    self.master.rowconfigure(0, weight = 1)
    self.master.columnconfigure(0, weight = 1)
    for i in xrange(4):
        self.rowconfigure(i, weight = 1)
    for i in xrange(3):
        self.columnconfigure(i, weight = 1)

def showHelp(self):
    showinfo("About", "The One Armed Bandit is a simplistic slot machine game")
def newStartingValue(self):
    self._optionsPanel = Toplevel(self.root)
    self._optionsPanel.title("Options")
    self._optionsPanel.grid()
    self._optionsPanelLabel = Label(self, text = "New Pot Starting Value").pack()
    self._optionsPanelLabel.grid(row=0,column=1)

I tried to only show necessary details. I think this should help you to figure it out. If not I can paste all code if needed. I can not understand why the label is not being put into the self._optionsPanel object.

Was it helpful?

Solution

In the following line, the code creates Label widget inside self (which is the frame inside the main window).

self._optionsPanelLabel = Label(self, text = "New Pot Starting Value").pack()

Replace it with (set new toplevel as its parent):

self._optionsPanelLabel = Label(self._optionsPanel, text = "New Pot Starting Value").pack()

OTHER TIPS

There are at least three problems in the last two lines of code:

self._optionsPanelLabel = Label(self, text = "New Pot Starting Value").pack()
self._optionsPanelLabel.grid(row=0,column=1)

First, you're passing self as the parent to Label. If you want it to appear on self._optionsPanel, you have to pass that as the parent.

Second, pack returns None, so self._optionsPanelLabel will be None, so the attempt to call grid will print an exception traceback to stderr and exit the function immediately. I'm willing to bet you've made that same mistake multiple other times in your code, so you probably have all kinds of things hooked up wrong.

Third, you can't call pack and grid on the same widget. Or, rather, you can, but once you do, the pack is undone. (And this breaks any other widgets packed in the same container, so calling both pack and grid on multiple widgets is an even bigger problem.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top