Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python Tkinter 버튼의 배경색을 재설정하는 방법은 무엇입니까?

<시간/>

Tkinter 버튼은 애플리케이션 내에서 이벤트를 처리하는 데 유용합니다. 버튼을 구성할 수 있습니다. 미리 정의된 속성을 사용하여 텍스트 스타일, 글꼴 모음, 배경색, 텍스트 색상 및 텍스트 크기와 같은 속성을 지정합니다.

콜백 함수를 정의하여 배경색 및 기타 속성을 재설정할 수 있습니다.

예시

#Import the tkinter library
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame
win= Tk()
#Define the geometry of the function
win.geometry("750x250")
#Define a function to change the properties of button
def change_color():
   btn.configure(bg="OrangeRed3", fg= "white")
#Create a Label
Label(win, text= "Click the Button to reset the Color of the Button", font= ('Georgia 16 italic')).pack(pady=30)
#Create a button to close the window
btn = Button(win, text ="RESET", command=change_color, font=('Georgia 11'))
btn.pack(side=TOP, ipady=5, ipadx=20)
win.mainloop()

출력

위의 코드를 실행하면 버튼과 그 안에 텍스트가 포함된 창이 표시됩니다.

Python Tkinter 버튼의 배경색을 재설정하는 방법은 무엇입니까?

이제 "RESET" 버튼을 클릭하여 버튼의 배경색과 전경색을 변경합니다.

Python Tkinter 버튼의 배경색을 재설정하는 방법은 무엇입니까?