如何設定大小固定的 Tkinter 視窗?
有時, Tkinter 框架會根據小元件的大小自動調整大小。為了使框架尺寸固定,我們必須停止小部件調整框架大小。所以有以下三種方法:
布林值 pack_propagate(True/False)方法防止小部件調整框架大小。
resize(x,y)方法防止視窗調整大小。
Pack(fill, expand)值會將視窗調整為其在幾何中的定義大小。
從本質上說,Tkinter 框架內的所有小元件都將具有響應性,並且無法調整大小。
示例
from tkinter import * win= Tk() win.geometry("700x300") #Don't allow the screen to be resized win.resizable(0,0) label= Label(win, text= "Select an option", font=('Times New Roman',12)) label.pack_propagate(0) label.pack(fill= "both",expand=1) def quit(): win.destroy() #Create two buttons b1= Button(win, text= "Continue") b1.pack_propagate(0) b1.pack(fill="both", expand=1) b2= Button(win, command= quit, text= "Quit") b2.pack_propagate(0) b2.pack(fill="both", expand=1) win.mainloop()
輸出
執行上述程式碼將使視窗的大小保持不變,不可調整大小。
廣告