Python的GUI编程(十二)Toplevel(顶层)
程序员文章站
2022-04-12 10:27:47
...
Tkinter Toplevel:顶层部件的工作,直接由窗口管理器管理的窗口。他们不必在它们上面的父widget
顶层部件的工作,直接由窗口管理器管理的窗口。他们不必在它们上面的父widget.
你的应用程序可以使用任意数量的顶层窗口.
语法:
这里是一个简单的语法来创建这个widget:
w = Toplevel ( option, ... )
参数:
-
options: 下面是这个小工具最常用的选项列表。这些选项可以作为键 - 值对以逗号分隔.
Option | Description |
---|---|
bg | The background color of the window. |
bd | Border width in pixels; default is 0. |
cursor | The cursor that appears when the mouse is in this window. |
class_ | Normally, text selected within a text widget is exported to be the selection in the window manager. Set exportselection=0 if you don't want that behavior. |
font | The default font for text inserted into the widget. |
fg | The color used for text (and bitmaps) within the widget. You can change the color for tagged regions; this option is just the default. |
height | Window height. |
relief | Normally, a top-level window will have no 3-d borders around it. To get a shaded border, set the bd option larger that its default value of zero, and set the relief option to one of the constants. |
width | The desired width of the window. |
方法:
量表的对象有这些方法:
Methods & Description |
---|
deiconify() Displays the window, after using either the iconify or the withdraw methods. |
frame() Returns a system-specific window identifier. |
group(window) Adds the window to the window group administered by the given window. |
iconify() Turns the window into an icon, without destroying it. |
protocol(name, function) Registers a function as a callback which will be called for the given protocol. |
iconify() Turns the window into an icon, without destroying it. |
state() Returns the current state of the window. Possible values are normal, iconic, withdrawn, and icon. |
transient([master]) Turns the window into a temporary(transient) window for the given master, or to the window's parent, when no argument is given. |
withdraw() Removes the window from the screen, without destroying it. |
maxsize(width, height) Defines the maximum size for this window. |
minsize(width, height) Defines the minimum size for this window. |
positionfrom(who) Defines the position controller. |
resizable(width, height) Defines the resize flags, which control whether the window can be resized. |
sizefrom(who) Defines the size controller. |
title(string) Defines the window title. |
以上来源:http://www.cnblogs.com/tkinter/p/5628832.html
创建简单的Toplevel
from Tkinter import *
root = Tk()
tl = Toplevel()
#为了区别root和tl,我们向tl中添加了一个Label
Label(tl,text = 'hello label').pack()
root.mainloop()
运行结果生成了两个窗体,一个是root启动的,另一个则是Toplevel创建的,它包含有一个label;关闭tl
则没有退出程序,Tk仍旧工作;若关闭Tk,整个Tk结束tl也结束,它不能单独存在。
虽然在上面tl与root没有相互作用,但是必须要有root对象
使用Toplevel自己制作提示框
from Tkinter import *
root = Tk()
mbYes,mbYesNo,mbYesNoCancel,mbYesNoAbort = 0,1,2,4
#定义一个消息对话框,依据传入的参数不同,弹出不同的提示信息
def MessageBox(): #没有使用参数
mbType = mbYesNo
textShow = 'Yes'
if mbType == mbYes:
textShow = 'Yes'
elif mbType == mbYesNo:
textShow = 'YesNo'
elif mbType == mbYesNoCancel:
textShow = 'YesNoCancel'
elif mbType == mbYesNoAbort:
textShow = 'YesNoAbort'
tl = Toplevel(height = 200,width = 400)
Label(tl,text = textShow).pack()
#由Button来启动这个消息框,因为它使用了空的回调函数,故MessageBox改为了无参数形式,使用了固定
#值mbYesNo
Button(root,text = 'press',command = MessageBox).pack()
root.mainloop()