简体   繁体   English

Python Tkinter顶层

[英]Python Tkinter Toplevel

I am working on a program that requires multiple windows, and the first one to appear is the login window, I used the Toplevel widget in order to make other windows its children, but this code keeps showing two windows instead of one. 我正在一个需要多个窗口的程序上工作,出现的第一个窗口是登录窗口,我使用了Toplevel小部件以使其他窗口成为其子窗口,但是此代码始终显示两个窗口而不是一个窗口。

from Tkinter import Frame, Toplevel
from ttk import Label, Entry, Button


class loginWindow(Toplevel):
    def __init__(self):
        Toplevel.__init__(self)
        self.title("Title")
        self.frame = Frame(self)
        self.frame.pack()
        self.__make_layout()
        self.mainloop()

    def __make_layout(self):
        self.frame.user_name_label = Label(text="User name:")
        self.frame.user_name_text = Entry()
        self.frame.user_name_label.grid(row=0, column=0)
        self.frame.user_name_text.grid(row=0, column=1)
        self.frame.password_label = Label(text="Password:")
        self.frame.password_text = Entry()
        self.frame.password_label.grid(row=1, column=0)
        self.frame.password_text.grid(row=1, column=1)
        self.frame.login_button = Button(text="Login")# , command=self.__create_window)
        self.frame.login_button.grid(row=2, column=0, columnspan=2)


if __name__ == '__main__':
    win1 = loginWindow()

All of the widgets created in _make_layout are created without a parent. _make_layout中创建的所有小部件都是在没有父_make_layout情况下创建的。 This means they're children of the default root. 这意味着它们是默认根目录的子级。 You need to pass a parent to each of them, the same way you do to the Frame . 您需要将父级传递给每个父级,就像传递给Frame Like this: 像这样:

self.frame.user_name_label = Label(self.frame, text="User name:")
self.frame.user_name_text = Entry(self.frame)
# etc.

When I run your exact code, I don't get a second window, on any platform I try. 当我运行您的确切代码时,在尝试使用的任何平台上都没有第二个窗口。 The closest I get is on OS X, where an entry for the default root window appears in the Window menu, but the window itself still doesn't appear and the widgets all end up on the Toplevel (although not on the Frame where you wanted them). 我得到的最接近的是OS X,默认的根窗口条目出现在“窗口”菜单中,但是窗口本身仍然没有出现,并且所有小部件最终都位于“ Toplevel (尽管不在您想要的Frame上)他们)。 But it certainly would be legal for Tkinter to show a second window here, and put some or all of your widgets on it. 但是Tkinter在这里显示第二个窗口并在其中放置部分或全部小部件当然是合法的。

This must be a platform dependent issue, since abarnert isn't having issues with multiple windows. 这必须是平台相关的问题,因为abarnert的多个窗口没有问题。 I use OS X with XQuartz and the following code gives me two windows: 我将OS X与XQuartz一起使用,以下代码为我提供了两个窗口:

from Tkinter import Toplevel, Tk
Toplevel().mainloop()

However, this code gives me one window: 但是,这段代码给了我一个窗口:

from Tkinter import Toplevel, Tk
Tk().mainloop()

I believe your first window should be declared Tk() and subsequent windows should be Toplevel() . 我相信您的第一个窗口应声明为Tk() ,随后的窗口应声明为Toplevel()

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

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