Python Forum
[Tkinter] changing background color of a button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] changing background color of a button
#1
I am a beginner to Python but not to programming in general.

I have the following code for creating 3 buttons:
for i in range(1, 3):
    aButton = "button" + str(i)
    aButton = tkinter.Button(Gui, font=Font1, text=i, image=myPixel, width=70, height=70, compound="c", bg='white', activebackground='white')
    aButton['command'] = partial(setBG, aButton)
    aButton.grid(row=1, column=i)
And it works. I get a three buttons.

Now, I want to use a function called ChangeColor to change the background color to red.
def ChangeColor():
    tkinter.button1['bg'] = 'red'
I run the script but I get
Error:
module 'tkinter' has no attribute 'button1'
How do I get a reference to any of the three buttons and change its background color?

.
Reply
#2
The way you have it written you are just dismissing the reference to each button
Quote:
for i in range(1, 3):
    aButton = "button" + str(i)
    aButton = tkinter.Button(Gui, font=Font1, text=i, image=myPixel, width=70, height=70, compound="c", bg='white', activebackground='white')
    aButton['command'] = partial(setBG, aButton)
    aButton.grid(row=1, column=i)
You only actually obtain the last button as aButton. You need a reference to each button to be able to change each button. So either, append each button to a list to save each reference. Loop the list and change each object color in the list as you please. OR use classes and call the attribute directly to change the color.
Recommended Tutorials:
Reply
#3
Thanks.

I created a list of buttons as you suggested.

buttons = []
then I add each button to that list
buttons.append(aButton)
But,
buttons[i]['bg'] = 'red'
does not work.
What is the correct syntax that I need to use?

.

Answering my own question.

I did not need
aButton = "button" + str(i)
.

for i in range(1, 3):
    aButton = tkinter.Button(Gui, font=Font1, text=i, image=myPixel, width=70, height=70, compound="c", bg='white', activebackground='white')
    buttons.append(aButton)
    aButton['command'] = partial(setBG, aButton)
    aButton.grid(row=1, column=i)
and add
		buttons[i].config(background="red")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't get tkinter button to change color based on changes in data dford 4 3,362 Feb-13-2022, 01:57 PM
Last Post: dford
  [Tkinter] changing and getting value through button niski1996 2 1,771 Aug-24-2021, 04:44 PM
Last Post: niski1996
Question [Tkinter] Can I set background color for each item in tkinter Combobox? water 1 5,055 Dec-10-2020, 07:48 PM
Last Post: Larz60+
  Tkinter - How can I remove the background borders from ttk.Button? TurboC 4 16,764 Oct-18-2020, 10:58 AM
Last Post: TurboC
  tkinter | Button color text on Click Maryan 2 3,314 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Trying to change font size w/o changing button size python63 3 9,729 Aug-05-2020, 01:04 AM
Last Post: Larz60+
  drawing concentric circles with changing color animation CatherineKan 0 2,411 Jul-05-2020, 03:30 PM
Last Post: CatherineKan
  [tkinter] color change for hovering over button teacher 4 8,328 Jul-04-2020, 06:33 AM
Last Post: teacher
  Restoring Tkinter widget background to original color pythonprogrammer 1 2,910 Dec-16-2019, 04:59 AM
Last Post: woooee
  Make Label Text background (default color) transparent using tkinter in python barry76 1 23,472 Nov-28-2019, 10:19 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