Tạo button, label trong Tkinter. Yêu cầu: text button tự resize theo kích thước cửa sổ chính.
Dùng Grid method: rowconfigure(), columnconfigure() để resize động. Dùng bind(' gán hàm xử lý khi cửa sổ thay đổi kích thước. Callback function nhận event, helper function điều chỉnh font size button.
Resize theo width trước, sau đó height.
Ví dụ
from tkinter import *
win = Tk()
win.geometry("700x300")
# Cấu hình grid resize động
Grid.rowconfigure(win, index=0, weight=1)
Grid.columnconfigure(win, index=0, weight=1)
def resize(e):
# Lấy width button
w = e.width / 10
# Resize font theo width
b.config(font=("Times New Roman", int(w)))
# Resize theo height
if e.height <= 300:
b.config(font=("Times New Roman", 30))
elif e.height < 100:
b.config(font=("Times New Roman", 10))
b = Button(win, text="Python")
b.grid(row=0, column=0, sticky="nsew")
win.bind('', resize)
win.mainloop() Kết quả
Chạy code tạo button "Python" tự resize text theo cửa sổ.
