Tkinter 버튼 색상을 변경하는 방법

Tkinter Button
위젯은 배경색과 전경색을 설정하기 위해bg
와fg
속성을 가지고 있습니다. Button
객체를 초기화 할 때bg
와fg
에 색상을 할당하고configure
메소드로 Tkinter Button
색상을 변경하거나bg
와fg
키에 새로운 값을 할당 할 수 있습니다.
Tkinter 버튼 색상 설정
import tkinter as tk
class Test():
def __init__(self):
self.root = tk.Tk()
self.root.geometry("250x100")
self.buttonA = tk.Button(self.root,
text = "Color",
bg = "blue",
fg = "red")
self.buttonB = tk.Button(self.root,
text="Click to change color",
bg = "gray",
fg = "purple")
self.buttonA.pack(side=tk.LEFT)
self.buttonB.pack(side=tk.RIGHT)
self.root.mainloop()
app=Test()
Tkinter Button
배경과 전경색을 설정하기 위해bg
를background
로,fg
를foreground
로 대체 할 수도 있습니다.
import tkinter as tk
class Test():
def __init__(self):
self.root = tk.Tk()
self.root.geometry("250x100")
self.buttonA = tk.Button(self.root,
text = "Color",
background = "blue",
foreground = "red")
self.buttonB = tk.Button(self.root,
text="Click to change color",
background = "gray",
foreground = "purple")
self.buttonA.pack(side=tk.LEFT)
self.buttonB.pack(side=tk.RIGHT)
self.root.mainloop()
app=Test()
configure
메소드로 Tkinter 버튼 색상 변경
Tkinter 버튼 위젯이 생성 된 후configure
메소드를 사용하여 색상을 변경할 수 있습니다.
import tkinter as tk
class Test():
def __init__(self):
self.root = tk.Tk()
self.root.geometry("250x100")
self.buttonA = tk.Button(self.root,
text = "Color",
bg = "blue",
fg = "red")
self.buttonB = tk.Button(self.root,
text="Click to change color",
command=self.changeColor)
self.buttonA.pack(side=tk.LEFT)
self.buttonB.pack(side=tk.RIGHT)
self.root.mainloop()
def changeColor(self):
self.buttonA.configure(bg="yellow")
app=Test()
self.buttonA.configure(bg="yellow")
background
또는 동일하게 bg
를 yellow
으로 구성합니다.
bg
/fg
속성으로 Tkinter Button
색상 변경
bg
와fg
는 Tkinter Button
위젯 객체 사전의keys
이므로 이러한 키의 새로운 값을 할당하여 Tkinter 버튼색상을 변경할 수 있습니다.
import tkinter as tk
class Test():
def __init__(self):
self.root = tk.Tk()
self.root.geometry("250x100")
self.buttonA = tk.Button(self.root,
text = "Color",
bg = "blue",
fg = "red")
self.buttonB = tk.Button(self.root,
text="Click to change color",
command=self.changeColor)
self.buttonA.pack(side=tk.LEFT)
self.buttonB.pack(side=tk.RIGHT)
self.root.mainloop()
def changeColor(self):
self.buttonA["bg"]="gray"
self.buttonA["fg"]="cyan"
app=Test()
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn관련 문장 - Tkinter Button
- Tkinter 버튼 명령에 인수를 전달하는 방법
- 버튼으로 Tkinter 창을 닫는 방법
- Tkinter 버튼 상태를 변경하는 방법
- Tkinter 버튼에 여러 명령을 바인딩하는 방법
- Tkinter 에서 버튼을 클릭하여 새 창을 만드는 방법
- Tkinter 버튼 크기를 변경하는 방법