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


Messages In This Thread
changing background color of a button - by ieee488 - Sep-06-2019, 11:52 AM

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,419 Feb-13-2022, 01:57 PM
Last Post: dford
  [Tkinter] changing and getting value through button niski1996 2 1,823 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,120 Dec-10-2020, 07:48 PM
Last Post: Larz60+
  Tkinter - How can I remove the background borders from ttk.Button? TurboC 4 17,021 Oct-18-2020, 10:58 AM
Last Post: TurboC
  tkinter | Button color text on Click Maryan 2 3,368 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Trying to change font size w/o changing button size python63 3 9,852 Aug-05-2020, 01:04 AM
Last Post: Larz60+
  drawing concentric circles with changing color animation CatherineKan 0 2,442 Jul-05-2020, 03:30 PM
Last Post: CatherineKan
  [tkinter] color change for hovering over button teacher 4 8,498 Jul-04-2020, 06:33 AM
Last Post: teacher
  Restoring Tkinter widget background to original color pythonprogrammer 1 2,969 Dec-16-2019, 04:59 AM
Last Post: woooee
  Make Label Text background (default color) transparent using tkinter in python barry76 1 23,763 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