Bottom Page

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.
Quote
#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()
        
Quote
#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 :-)
Quote
#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.

.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Restoring Tkinter widget background to original color pythonprogrammer 1 273 Dec-16-2019, 04:59 AM
Last Post: woooee
  Make Label Text background (default color) transparent using tkinter in python barry76 1 3,256 Nov-28-2019, 10:19 AM
Last Post: Larz60+
  [Tkinter] changing background color of a button ieee488 2 577 Sep-06-2019, 01:24 PM
Last Post: ieee488
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 664 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [WxPython] How to change font color of SetHint or SetDescriptiveText? anistorian 5 876 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,159 Apr-03-2019, 09:49 PM
Last Post: jpezz
  Can't change the colour of Tk button text Pilover 4 3,055 Feb-27-2019, 09:02 PM
Last Post: Pilover
  set button background with image of window background gray 3 8,784 Oct-18-2017, 08:22 AM
Last Post: gray
  Change color of a row of a QTableView? panoss 7 16,773 Sep-23-2017, 03:02 PM
Last Post: Mafireyi
  [Tkinter] Selected radio button in push button in Tkinter prashantfunde91 1 7,416 Jun-22-2017, 05:27 PM
Last Post: DeaD_EyE

Forum Jump:


Users browsing this thread: 1 Guest(s)