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"이라는 텍스트가 있는 버튼이 생성되고 이 버튼은 동적으로 크기를 조정할 수 있습니다.
