简体   繁体   English

使用Tkinter在子窗口内创建标签

[英]Creating a label inside of a child window with Tkinter

I don't know why this is happening but I'm trying to create an "Options" child window with Tkinter from a MenuBar. 我不知道为什么会发生这种情况,但我正在尝试使用来自MenuBar的Tkinter创建一个“选项”子窗口。 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. 我无法理解为什么标签没有放入self._optionsPanel对象。

In the following line, the code creates Label widget inside self (which is the frame inside the main window). 在下一行中,代码在self创建Label小部件(这是主窗口内的框架)。

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

Replace it with (set new toplevel as its parent): 替换为(将new toplevel设为其父级):

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

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 . 首先,您将self作为父级传递给Label If you want it to appear on self._optionsPanel , you have to pass that as the parent. 如果您希望它出现在self._optionsPanel ,则必须将作为父级传递。

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. 其次, pack返回None ,因此self._optionsPanelLabel将为None ,因此调用grid的尝试将打印异常回溯到stderr并立即退出该函数。 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. 第三,你不能在同一个小部件上调用packgrid Or, rather, you can, but once you do, the pack is undone. 或者,相反,你可以,但是一旦你这样做, pack就会撤消。 (And this breaks any other widgets pack ed in the same container, so calling both pack and grid on multiple widgets is an even bigger problem.) (这会打破在同一个容器中pack任何其他小部件,因此在多个小部件上调用packgrid是一个更大的问题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM