Python Forum
change background color of button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
change background color of button
#1
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.
Reply
#2
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.btn.pack()
        self.color = color

    def change_color(self):
        self.btn.config(bg=self.color)

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

root.mainloop()
        
Recommended Tutorials:
Reply
#3
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
        super().__init__(**kwargs)
        
        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()
 
root.mainloop()
Tested with Ubuntu 16.04 & Python 3.5

wuf :-)
Reply
#4
Thank you both.

I ended up with
def setBG(button):
    if button['bg'] == 'white':
        button['bg'] = 'yellow'
        button['activebackground'] = 'yellow'
    else:
        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.

.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't change the colour of Tk button text Pilover 6 14,505 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,766 Aug-23-2022, 09:11 PM
Last Post: Extra
  [WxPython] [SOLVED] How to change button label? Winfried 3 2,020 May-31-2022, 06:37 PM
Last Post: Winfried
  Tkinter - How can I change the default Notebook border color? TurboC 5 14,638 May-23-2022, 03:44 PM
Last Post: bigmac
Question [Tkinter] Change Treeview column color? water 3 9,425 Mar-04-2022, 11:20 AM
Last Post: Larz60+
  Can't get tkinter button to change color based on changes in data dford 4 3,364 Feb-13-2022, 01:57 PM
Last Post: dford
Question [Tkinter] Can I set background color for each item in tkinter Combobox? water 1 5,060 Dec-10-2020, 07:48 PM
Last Post: Larz60+
  Tkinter - How can I remove the background borders from ttk.Button? TurboC 4 16,778 Oct-18-2020, 10:58 AM
Last Post: TurboC
  tkinter | Button color text on Click Maryan 2 3,317 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Trying to change font size w/o changing button size python63 3 9,738 Aug-05-2020, 01:04 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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