简体   繁体   English

为什么ttk.Button而不是tk.Toplevel出现在主窗口中?

[英]Why does ttk.Button appears in the main window instead of tk.Toplevel?

I have a main UI which gets instantiated like this: 我有一个主用户界面,其实例化如下:

class MyApp(ttk.Frame):
    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        (...)

    def make_gui(self):
        (...)
        self.helpButt = ttk.Button(self.InnerRightFrm2, padding=(0, 0),
                                   text='Help', image=help_icon
                                   compound='left', command=self.show_help)
        (...)

def main():
    root = tk.Tk()
    root.title('Myapp')
    root.columnconfigure(0, weight=1)
    root.rowconfigure(0, weight=1)
    root.resizable(True, True)
    root.update()
    gui = MyApp(root)
    gui.mainloop()

When a user clicks Show help button located on the main interface a new Toplevel window should appear. 当用户单击主界面上的Show help按钮时,将出现一个新的Toplevel窗口。 The Toplevel of this window contains two frames: topFrame0 on row=0 and topFrame1 on row=1 . 所述Toplevel该窗口的包含两帧: topFrame0row=0topFrame1row=1 I create a third frame inside topFrame1 to put "Close" Button in in. Here is how I do it: 我在topFrame1内创建第三个框架,以将“关闭”按钮放入其中。这是我的操作方法:

def show_help():
    top_win = tk.Toplevel()
    top_win.title('Help')
    top_win.resizable(0, 0)

    topFrame0 = ttk.Frame(top_win, borderwidth=2, relief='groove')
    topFrame0.grid(row=0, column=0, sticky='nsew')
    topFrame1 = ttk.Frame(top_win, borderwidth=2, relief='flat')
    topFrame1.grid(row=1, column=0, sticky='nsew')
    buttonFrame = ttk.Frame(topFrame1, borderwidth=2, relief='groove').grid()
    ttk.Button(buttonFrame, padding=(0, 2), text='Close', command=top_win.destroy).grid(sticky='e')

However, instead of appearing in the right botton of Toplevel window (notice red arrow in the screenshot), "Close" Button appears in the right bottom of the main MyApp window! 但是,不是出现在“ Toplevel窗口的右底部(屏幕快照中的红色箭头),而是“关闭”按钮出现在MyApp主窗口的右下角! How can this even happen? 这怎么可能发生?

在此处输入图片说明

In line 排队

buttonFrame = ttk.Frame(...).grid()

you assign None to buttonFrame because grid() returns None so you have later ttk.Button(None, ...) and you add button to main window. 您将None分配给buttonFrame因为grid()返回None所以您稍后ttk.Button(None, ...)并将按钮添加到主窗口。

You need 你需要

buttonFrame = ttk.Frame(...)
buttonFrame.grid()

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

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