📜  Python Tkinter顶层窗口

📅  最后修改于: 2020-10-27 02:02:10             🧑  作者: Mango

Tkinter顶层窗口

顶层窗口小部件用于创建和显示由窗口管理器直接管理的顶层窗口。顶层窗口小部件的顶部可能带有父窗口,也可能没有。

当Python应用程序需要在新窗口中表示一些额外的信息,弹出窗口或一组窗口小部件时,将使用顶层窗口小部件。

顶层窗口具有标题栏,边框和其他窗口装饰。

下面给出了使用“顶级”窗口小部件的语法。

句法

w = Toplevel(options) 

下面列出了可能的选项。

SN Options Description
1 bg It represents the background color of the window.
2 bd It represents the border size of the window.
3 cursor The mouse pointer is changed to the cursor type set to the arrow, dot, etc. when the mouse is in the window.
4 class_ The text selected in the text widget is exported to be selected to the window manager. We can set this to 0 to make this behavior false.
5 font The font type of the text inserted into the widget.
6 fg The foreground color of the widget.
7 height It represents the height of the window.
8 relief It represents the type of the window.
9 width It represents the width of the window,

方法

下面的列表中给出了与Toplevel小部件关联的方法。

SN Method Description
1 deiconify() This method is used to display the window.
2 frame() It is used to show a system dependent window identifier.
3 group(window) It is used to add this window to the specified window group.
4 iconify() It is used to convert the toplevel window into an icon.
5 protocol(name, function) It is used to mention a function which will be called for the specific protocol.
6 state() It is used to get the current state of the window. Possible values are normal, iconic, withdrawn, and icon.
7 transient([master]) It is used to convert this window to a transient window (temporary).
8 withdraw() It is used to delete the window but doesn’t destroy it.
9 maxsize(width, height) It is used to declare the maximum size for the window.
10 minsize(width, height) It is used to declare the minimum size for the window.
11 positionfrom(who) It is used to define the position controller.
12 resizable(width, height) It is used to control whether the window can be resizable or not.
13 sizefrom(who) It is used to define the size controller.
14 title(string) It is used to define the title for the window.

from tkinter import *

root = Tk()

root.geometry("200x200")

def open():
    top = Toplevel(root)
    top.mainloop()

btn = Button(root, text = "open", command = open)

btn.place(x=75,y=50)

root.mainloop()

输出: