สมมติว่าเราได้สร้างปุ่มและป้ายกำกับใน Tkinter Frame แล้ว งานคือการอนุญาตให้ข้อความปุ่มปรับขนาดเป็นหน้าต่างหลักแบบไดนามิก เราสามารถสร้างปุ่มโดยใช้ วิดเจ็ตปุ่ม . อย่างไรก็ตาม ยังมีฟังก์ชันอื่นๆ อีกหลายอย่างที่ใช้สร้างป้ายกำกับปุ่มแบบไดนามิก
ในตัวอย่างนี้ เราจะสร้างปุ่มสองปุ่มที่มีป้ายกำกับอยู่ โดยใช้วิธีกริด เช่น rowconfigure() และ columnconfigure() เราจะปรับขนาดหน้าต่างหลักหรือรูทแบบไดนามิก
ในการทำให้ข้อความปุ่มเป็นแบบไดนามิก เราจะใช้ bind(
ขั้นแรก เราจะปรับขนาดข้อความของปุ่มตามความกว้างแล้วตามด้วยความสูง
ตัวอย่าง
from tkinter import *
win= Tk()
win.geometry("700x300")
#Dynamically resize the window and its widget
Grid.rowconfigure(win, index=0, weight=1)
Grid.columnconfigure(win, index=0, weight=1)
#Define the function to change the size of the button text
def resize(e):
#Get the width of the button
w= e.width/10
#Dynamically Resize the Button Text
b.config(font=("Times New Roman",int(w)))
#Resize the height
if e.height <=300:
b.config(font= ("Times New Roman",30))
elif e.height<100:
b.config(font= ("Time New Roman", 10))
#Let us Create buttons,
b=Button(win,text="Python")
b.grid(row= 0, column=0, sticky= "nsew")
win.bind('<Configure>', resize)
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะสร้างปุ่มที่มีข้อความ "Python" และปุ่มนี้สามารถปรับขนาดแบบไดนามิกได้
