如何在 Python Tkinter 中為 Frame 新增邊框?


要在Tkinter中為Frame新增邊框,我們必須在建立Frame時使用highlightbackgroundhighlightthickness引數。讓我們來看一個例子,看看如何使用這兩個引數。

步驟:

  • 匯入tkinter庫並建立一個tkinter frame的例項。

  • 使用geometry()方法設定frame的大小。

  • 使用Frame()方法建立一個frame。用顏色突出顯示frame的邊框,highlightbackground="blue"。然後,設定邊框的厚度,highlightthickness=2

  • 接下來,在frame內建立一些widgets。在這個例子中,我們在frame內放置了四個複選框和一個按鈕

  • 最後,執行應用程式視窗的mainloop

示例

from tkinter import *

top = Tk()
top.geometry("700x350")

frame1 = Frame(top, highlightbackground="blue", highlightthickness=2)
frame1.pack(padx=20, pady=20)

C1 = Checkbutton(frame1, text = "Music", width=200, anchor="w")
C1.pack(padx=10, pady=10)

C2 = Checkbutton(frame1, text = "Video", width=200, anchor="w")
C2.pack(padx=10, pady=10)

C3 = Checkbutton(frame1, text = "Songs", width=200, anchor="w")
C3.pack(padx=10, pady=10)

C4 = Checkbutton(frame1, text = "Games", width=200, anchor="w")
C4.pack(padx=10, pady=10)

Button(frame1, text="Button-1", font=("Calibri",12,"bold")).pack(padx=10, pady=10)

top.mainloop()

輸出

它將產生以下輸出:

有一種更簡單的方法來為frame建立一個簡單的邊框。與其使用Frame,不如建立一個LabelFrame,它會自動在frame widgets周圍設定邊框。

學習Python程式設計:Python 教程

學習Python程式設計

更新於:2023年8月28日

32K+ 瀏覽量

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.