Python Forum
change background color of button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
change background color of button
Starting to learn Python though I know other languages such as VB.NET

I create 5 buttons.
tkinter.Button(Gui, "1", command=callback).grid(row=0, column=1)

tkinter.Button(Gui, "5", command=callback).grid(row=0, column=5)
When I click on any of the 5 buttons, I want to change its background color.

I don't know how to write a generic callback function that changes the background color for only the button that was clicked.
This method uses classes
import tkinter as tk

root = tk.Tk()

class Button:
    def __init__(self, win, text, color):
        self.btn = tk.Button(win, text=text, command=self.change_color)
        self.color = color

    def change_color(self):

btn1 = Button(root, 'one', '#00ffff')
btn2 = Button(root, 'two', '#0000ff')

Hi ieee488

Here a further variant to play around with:
import tkinter as tk
root = tk.Tk()

def button_callback(state):
    print('Button Callback', state)
class Button(tk.Button):
    def __init__(self, parent, active_color=None, **kwargs):
        self.active_color = active_color
        self.callback = kwargs['command'] if 'command' in kwargs else None
        self['command'] = self.change_color
        self.bg_color = self['bg']
        self.state = False
    def change_color(self):
        self.state = not self.state
        self['bg'] = self.active_color if self.state else self.bg_color
        if self.callback: self.callback(self.state)
Button(root, '#00ffff', text='one', command=button_callback).pack()
Button(root, '#90ee90', text='two').pack()
Button(root, text='three').pack()
Tested with Ubuntu 16.04 & Python 3.5

wuf :-)
Thank you both.

I ended up with
def setBG(button):
    if button['bg'] == 'white':
        button['bg'] = 'yellow'
        button['activebackground'] = 'yellow'
        button['bg'] = 'white'
        button['activebackground'] = 'white'
where the buttons are created with
aButton = tkinter.Button(Gui, bg='white', activebackground='white')
aButton['command'] = partial(setBG, aButton)
This was something quick, though using class is definitely the better way.
I have a lot to learn with Python.


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [Tkinter] Can I set background color for each item in tkinter Combobox? water 1 1,064 Dec-10-2020, 07:48 PM
Last Post: Larz60+
  Tkinter - How can I remove the background borders from ttk.Button? TurboC 4 2,861 Oct-18-2020, 10:58 AM
Last Post: TurboC
  tkinter | Button color text on Click Maryan 2 599 Oct-09-2020, 08:56 PM
Last Post: Maryan
  Tkinter - How can I change the default Notebook border color? TurboC 4 2,612 Oct-06-2020, 10:35 PM
Last Post: TurboC
  [Tkinter] Trying to change font size w/o changing button size python63 3 3,706 Aug-05-2020, 01:04 AM
Last Post: Larz60+
  [tkinter] color change for hovering over button teacher 4 2,437 Jul-04-2020, 06:33 AM
Last Post: teacher
  [PyQt] Increase text size and change color based on temp pav1983 5 732 Jun-22-2020, 10:52 PM
Last Post: menator01
  TKINTER - Change font color for night or day Ayckinn 2 1,170 May-24-2020, 09:25 PM
Last Post: Ayckinn
  Restoring Tkinter widget background to original color pythonprogrammer 1 1,017 Dec-16-2019, 04:59 AM
Last Post: woooee
  Make Label Text background (default color) transparent using tkinter in python barry76 1 11,971 Nov-28-2019, 10:19 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020