Tkinter 中的 maxsize() 方法 | Python
该方法用于设置根窗口的最大尺寸(一个窗口可以展开的最大尺寸)。用户仍然可以将窗口的大小缩小到最小。
句法 :
master.maxsize(height, width)
这里,高度和宽度以像素为单位。
代码#1:
# importing only those functions
# which are needed
from tkinter import *
from tkinter.ttk import *
from time import strftime
# creating tkinter window
root = Tk()
# Adding widgets to the root window
Label(root, text = 'GeeksforGeeks',
font =('Verdana', 15)).pack(side = TOP, pady = 10)
Button(root, text = 'Click Me !').pack(side = TOP)
mainloop()
输出 :
窗口的初始大小(未设置窗口的最大大小)
窗口的扩展大小(此窗口可以扩展至屏幕大小,因为大小不固定)。
代码 #2:修复根窗口的最大尺寸
# importing only those functions
# which are needed
from tkinter import *
from tkinter.ttk import *
from time import strftime
# creating tkinter window
root = Tk()
# Fixing the size of the root window.
# No one can now expand the size of the
# root window than the specified one.
root.maxsize(200, 200)
# Adding widgets to the root window
Label(root, text = 'GeeksforGeeks',
font =('Verdana', 15)).pack(side = TOP, pady = 10)
Button(root, text = 'Click Me !').pack(side = TOP)
mainloop()
输出 :
窗口的最大展开尺寸
注意: Tkinter 还提供了一个 minsize() 方法,用于设置窗口的最小尺寸。