Tkinter 中的 resizable() 方法Python
resizable()
方法用于允许 Tkinter 根窗口根据用户需要更改其大小,我们也可以禁止调整 Tkinter 窗口的大小。
所以,基本上,如果用户想创建一个固定大小的窗口,可以使用这种方法。
如何使用:
-> import tkinter
-> root = Tk()
-> root.resizable(height = None, width = None)
要传递的参数:
-> In resizable()
method user can pass either positive integer or True, to make the window resizable.
-> To make window non-resizable user can pass 0 or False.
代码#1:允许根窗口改变它的大小
# importing only those functions
# which are needed
from tkinter import *
from tkinter.ttk import *
from time import strftime
# creating tkinter window
root = Tk()
root.title('Resizable')
root.geometry('250x100')
Label(root, text = 'It\'s resizable').pack(side = TOP, pady = 10)
# Allowing root window to change
# it's size according to user's need
root.resizable(True, True)
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()
root.title('Resizable')
root.geometry('250x100')
Label(root, text = 'It\'s non-resizable').pack(side = TOP, pady = 10)
# Restricting root window to change
# it's size according to user's need
root.resizable(0, 0)
mainloop()
输出:
您可能会观察到蓝色圆圈内的部分被禁用,即无法更改窗口的大小。