如何在Python Tkinter中框架周围放置边框?

如何在Python Tkinter中框架周围放置边框?

要在Tkinter中的框架周围放置边框,我们必须在创建框架时使用 highlightbackgroundhighlightthickness 参数。让我们举个例子,看看如何使用这两个参数。

步骤:

  • 导入tkinter库并创建tkinter框架的实例。

  • 使用 geometry 方法设置框架的大小。

  • 使用 Frame() 方法创建框架。使用颜色 highlightbackground=”blue” 突出框架的边界。然后,设置边框的厚度 highlightthickness=2

  • 接下来,在框架内创建一些小部件。在示例中,我们将四个 checkbuttons 和一个 button 放置在框架内。

  • 最后,运行应用程序窗口的 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()

输出

它将产生以下输出−

如何在Python Tkinter中框架周围放置边框?

有一种更简便的方法可以在框架周围创建简单的边框。而不是使用 Frame ,请创建一个 LabelFrame ,它将自动在框架小部件周围设置边框。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程