Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách tạo viền cho Frame trong Tkinter

Dùng tham số highlightbackground (màu viền) và highlightthickness (độ dày) khi tạo Frame. Hoặc dùng LabelFrame tự động có viền.

Ví dụ 1: Frame với highlightbackground/thickness

from tkinter import *

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

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

Checkbutton(frame1, text="Music", width=200, anchor="w").pack(padx=10, pady=10)
Checkbutton(frame1, text="Video", width=200, anchor="w").pack(padx=10, pady=10)
Checkbutton(frame1, text="Songs", width=200, anchor="w").pack(padx=10, pady=10)
Checkbutton(frame1, text="Games", width=200, anchor="w").pack(padx=10, pady=10)
Button(frame1, text="Button-1", font=("Calibri",12,"bold")).pack(padx=10, pady=10)

top.mainloop()

Kết quả

Frame có viền xanh bao quanh các widget bên trong.

Cách tạo viền cho Frame trong Tkinter

Cách dễ hơn: LabelFrame

Thay Frame bằng LabelFrame sẽ tự động có viền và tiêu đề.