如何更改 Tkinter 按钮颜色

Jinku Hu 2023年1月30日 2020年6月9日
  1. 设置 Tkinter Button 颜色
  2. configure 方法改变 Tkinter Button 颜色
  3. 使用 bg /fg 属性更改 Tkinter Button 颜色
如何更改 Tkinter 按钮颜色

Tkinter Button 控件具有属性 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 按钮设置背景和前景颜色

你也可以将 bg 替换为 background,将 fg 替换为 foreground 来设置 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")

它将背景 bg 配置为黄色。

使用 bg /fg 属性更改 Tkinter Button 颜色

bgfg 是 Tkinter Button 控件对象字典的键值,因此,我们可以通过为这些键指定新的值来更改 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