如何冻结 Tkinter 窗口框架的大小
在某些情况下,我们希望冻结 Tkinter 窗口的大小,或者换句话说,该框架不可调整大小。例如,无论框架中的标签窗口控件太长还是太短,窗口框架都保持不变。
框架 resizable
方法
resizable(width= , height=)
可将窗口尺寸中的 width
和 height
配置成是否可变。
resizable(width = False)
仅冻结窗口宽度,resizable(height = False)
仅冻结窗口高度。使用 resizable(width=False, height=False)
将冻结整个窗口的大小,或者使用更为简洁的 resizable(False, False)
。
try:
import Tkinter as tk
except:
import tkinter as tk
app = tk.Tk()
app.title("Frame Window Size Frozen")
app.geometry('300x200')
app.resizable(width=False, height=False)
app.mainloop()
框架 minsize
和 maxsize
方法
minsize
和 maxsize
方法通常用于设置最小和最大窗口大小,但是如果将最小和最大大小设置为相同,则也实现冻结窗口大小的目的。
try:
import Tkinter as tk
except:
import tkinter as tk
app = tk.Tk()
app.title("Frame Window Size Frozen")
app.minsize(width=600, height=400)
app.maxsize(width=600, height=400)
app.mainloop()
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn