Bottom Page

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.


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [tkinter] color change for hovering over button teacher 4 94 Jul-04-2020, 06:33 AM
Last Post: teacher
  [PyQt] Increase text size and change color based on temp pav1983 5 152 Jun-22-2020, 10:52 PM
Last Post: menator01
  TKINTER - Change font color for night or day Ayckinn 2 320 May-24-2020, 09:25 PM
Last Post: Ayckinn
  Restoring Tkinter widget background to original color pythonprogrammer 1 464 Dec-16-2019, 04:59 AM
Last Post: woooee
  Make Label Text background (default color) transparent using tkinter in python barry76 1 6,376 Nov-28-2019, 10:19 AM
Last Post: Larz60+
  [Tkinter] changing background color of a button ieee488 2 846 Sep-06-2019, 01:24 PM
Last Post: ieee488
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 957 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [WxPython] How to change font color of SetHint or SetDescriptiveText? anistorian 5 1,128 Jun-19-2019, 05:30 PM
Last Post: Yoriz
  [Tkinter] So what do I need to do change the color this label? jpezz 10 1,632 Apr-03-2019, 09:49 PM
Last Post: jpezz
  Can't change the colour of Tk button text Pilover 4 4,210 Feb-27-2019, 09:02 PM
Last Post: Pilover

Forum Jump:

Users browsing this thread: 1 Guest(s)