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


Messages In This Thread
change background color of button - by ieee488 - Jul-27-2019, 11:21 PM
RE: change background color of button - by wuf - Jul-28-2019, 08:44 AM
RE: change background color of button - by ieee488 - Jul-30-2019, 09:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't change the colour of Tk button text Pilover 6 14,748 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,896 Aug-23-2022, 09:11 PM
Last Post: Extra
  [WxPython] [SOLVED] How to change button label? Winfried 3 2,091 May-31-2022, 06:37 PM
Last Post: Winfried
  Tkinter - How can I change the default Notebook border color? TurboC 5 14,772 May-23-2022, 03:44 PM
Last Post: bigmac
Question [Tkinter] Change Treeview column color? water 3 9,608 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,419 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,122 Dec-10-2020, 07:48 PM
Last Post: Larz60+
  Tkinter - How can I remove the background borders from ttk.Button? TurboC 4 17,036 Oct-18-2020, 10:58 AM
Last Post: TurboC
  tkinter | Button color text on Click Maryan 2 3,371 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Trying to change font size w/o changing button size python63 3 9,855 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