Tkinter ボタンの色を変更する方法

Jinku Hu 2023年1月30日 2020年6月13日
  1. Tkinter Button の色を設定する
  2. configure メソッドで Tkinter の Button の色を変更する
  3. Tkinter の Button の色を bg/fg 属性で変更する
Tkinter ボタンの色を変更する方法

Tkinter のボタンウィジェットには、背景色と前景色を設定するための属性 bgfg があります。Button オブジェクトを初期化するときに bgfg に色を割り当て、configure メソッドで Tkinter Button の色を変更するか、新しい値を bgfg キーに割り当てることができます。

Tkinter Button の色を設定する

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 ボタンセットの背景と前景色

また、bgbackground に、fgforeground に置き換えて、Tkinter の Button の背景と前景色を設定することもできます。

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 の Button の色を変更する

Tkinter の Button ウィジェットが作成された後、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()

Tkinter セットボタンの色

self.buttonA.configure(bg="yellow")

background または bg黄色に設定します。

Tkinter の Button の色を bg/fg 属性で変更する

bgfg は Tkinter の Button ウィジェットオブジェクトディクショナリの keys なので、これらの keys に新しい値を割り当てることで Tkinter の Button の色を変更できます。

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()

Tkinter は bg および fg 属性でボタンの色を設定する

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

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