如何在沒有標題欄的情況下用 Tkinter 建立一個可調整的視窗?
若要建立沒有任何標題欄的 Tkinter 視窗,我們可以使用 overrideredirect(布林) 屬性,它從 Tkinter 視窗頂部停用導航面板。然而,它不會允許使用者立即調整視窗大小。
如果我們需要以程式設計方式建立一個沒有標題欄的可調整大小的視窗,那麼我們可以在 Tkinter 中使用 Sizegrip(parent) 小元件。Sizegrip 小元件可為應用程式新增可擴充套件性,允許使用者拉動並調整主視窗的大小。要使用 Sizegrip 小元件,我們必須繫結滑鼠按鈕和一個函式,在拉動該抓取點時調整視窗大小。
示例
# Import the required libraries from tkinter import * from tkinter import ttk # Create an instance of tkinter frame or window win=Tk() # Set the size of the window win.geometry("700x350") # Remove the Title bar of the window win.overrideredirect(True) # Define a function for resizing the window def moveMouseButton(e): x1=winfo_pointerx() y1=winfo_pointery() x0=winfo_rootx() y0=winfo_rooty() win.geometry("%s x %s" % ((x1-x0),(y1-y0))) # Add a Label widget label=Label(win,text="Grab the lower-right corner to resize the window") label.pack(side="top", fill="both", expand=True) # Add the gripper for resizing the window grip=ttk.Sizegrip() grip.place(relx=1.0, rely=1.0, anchor="se") grip.lift(label) grip.bind("<B1-Motion>", moveMouseButton) win.mainloop()
如果我們執行上面的程式碼,它將顯示一個沒有任何標題欄的視窗。我們可以透過拉動右下角的抓取點來調整此視窗的大小。
輸出
廣告