如何凍結 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