如何在Python Tkinter中给Frame添加边框?
要在Tkinter中给Frame添加边框,我们需要在创建Frame时使用highlightbackground和highlightthickeness参数。让我们来看一个例子,看看如何使用这两个参数。
步骤-
- 导入tkinter库并创建一个tkinter frame的实例。
-
使用geometry()方法设置框架的大小。
-
使用Frame()方法创建一个框架。用颜色突出显示框架的边框,highlightbackground=”blue”。然后,设置边框的厚度,highlightthickness=2。
-
接下来,在框架内创建一些小部件。在例子中,我们将四个checkbuttons和一个按钮放在框架内。
-
最后,运行应用程序窗口的主循环。
示例
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 ,而是创建一个 LabelFrame ,它会自动设置框架小部件周围的边框。