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

Đổi font mặc định cho tất cả widget Tkinter

Đổi font mặc định toàn ứng dụng bằng option_add("*Font", "font_name"). Các widget sau sẽ kế thừa font này.

Ví dụ

# Import thư viện
from tkinter import *

# Tạo khung
win = Tk()
win.geometry("700x350")

# Font mặc định cho tất cả widget
win.option_add("*Font", "aerial")
# Font riêng cho Label
win.option_add("*Label.Font", "aerial 18 bold")
# Màu nền mặc định
win.option_add("*Background", "bisque")

# Các widget
Label(win, text="Label").pack()
Button(win, text="Button").pack()

w = Listbox(win)
for i in range(5):
    w.insert(i, "item %d" % (i+1))
w.pack()

w = Text(win, width=20, height=10)
w.insert(1.0, "a text widget")
w.pack()

win.mainloop()

Kết quả

Label, Button, Listbox, Text đều dùng font aerial, Label dùng aerial 18 bold, nền bisque.

Đổi font mặc định cho tất cả widget Tkinter