Bottom Page

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?

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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Restoring Tkinter widget background to original color pythonprogrammer 1 220 Dec-16-2019, 04:59 AM
Last Post: woooee
  Make Label Text background (default color) transparent using tkinter in python barry76 1 2,169 Nov-28-2019, 10:19 AM
Last Post: Larz60+
  change background color of button ieee488 3 2,083 Jul-30-2019, 09:49 PM
Last Post: ieee488
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 576 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  Error while changing button relief from raised to sunken on left click mgtheboss 1 2,197 Jan-09-2018, 06:05 PM
Last Post: Gribouillis
  set button background with image of window background gray 3 8,367 Oct-18-2017, 08:22 AM
Last Post: gray
  [Tkinter] Selected radio button in push button in Tkinter prashantfunde91 1 7,175 Jun-22-2017, 05:27 PM
Last Post: DeaD_EyE
  [Tkinter] Problem with changing label text on button press xk2006x 1 3,132 Jun-02-2017, 06:00 PM
Last Post: nilamo
  PyGTK3, I can't Change Button and Table Backgorund Color using modify_bg method. harun2525 3 4,335 Apr-24-2017, 04:05 PM
Last Post: changos
  [Tkinter] Change background color sparkz_alot 3 18,587 Dec-10-2016, 02:15 PM
Last Post: sparkz_alot

Forum Jump:


Users browsing this thread: 1 Guest(s)