如何在 Tkinter 中去除部件邊框?
Tkinter 帶有不同型別的部件,例如 Button、Entry、Frame、Label、Radiobutton、Scrollbar 等。部件是標準的圖形使用者介面 (GUI) 元素,用於顯示資訊或幫助使用者與系統互動。
在本例中,我們將瞭解如何從畫布、Entry 欄位、標籤和按鈕中去除邊框。
步驟 -
匯入所需的庫並建立 Tkinter 框架的例項。
使用 **root.geometry** 方法設定框架的大小。
接下來,建立一個畫布並使用 **"bd"** 屬性設定畫布的邊框寬度。然後,使用 **"highlightthickness"** 屬性定義是否要顯示畫布邊框。如果要去除畫布邊框,則將其設定為 **"highlightthickness=0"**。
定義另外兩個使用者自定義函式 **start()** 和 **stop()** 來控制 **infinite_loop**。定義一個全域性變數 **"condition"**。在 **start()** 中,將 condition 設定為 True,在 **stop()** 中,將 **condition** 設定為 False。
接下來,在畫布內建立兩個 Entry 欄位。使用 **borderwidth** 屬性設定其中一個 Entry 欄位的邊框。
類似地,建立兩個標籤,並使用 **borderwidth** 屬性和 **relief='solid'** 在標籤周圍顯示邊框。
接下來,建立兩個按鈕,並在其中一個按鈕中設定 **"borderwidth=0"**。這將去除按鈕周圍的邊框。
最後,執行應用程式視窗的 **mainloop**。
示例
# Import the required libraries from tkinter import * #Create an instance of tkinter frame root=Tk() # Set the geometry of frame root.geometry("700x350") # Create a canvas widget canvas= Canvas(root, bd=2, highlightthickness=2) canvas.pack(side=TOP, padx=10, pady=10) # Create an Entry widget text=Entry(canvas, width=50) text.insert(0, "Widget with border") text.config(borderwidth=5) text.pack(side=TOP, padx=10, pady=10) # Create Entry widget without border text=Entry(canvas, width=50) text.insert(0, "Widget without border") text.pack(side=TOP, padx=10, pady=10) label1 = Label(canvas, text="Label with border", borderwidth=2, relief='solid', font="Calibri, 14") label1.pack(side=BOTTOM, padx=10, pady=10) label2 = Label(canvas, text="Label without border", borderwidth=0, font="Calibri, 14") label2.pack(side=BOTTOM, padx=10, pady=10) button1 = Button(root, text="Standard Button") button1.pack(side=TOP, padx=10, pady=10) button2 = Button(root, text="Button without Border", borderwidth=0) button2.pack(side=TOP, padx=10, pady=10) root.mainloop()
輸出
執行後,將生成以下輸出 -
觀察畫布部件是否有邊框。如果在畫布中將屬性 **"highlightthickness=0"** 設定為,則它將不再顯示周圍的邊框。
類似地,我們有兩個按鈕,一個帶邊框,另一個不帶邊框。要刪除 **button** 部件中的邊框,我們使用了引數 **"borderwidth=0"**。