0

I want the information to be entered when I press a button with a red background. / But it doesn't work. / How can I make it enter when the button background is red?

color= ['red', 'orange', 'yellow', 'green', 'blue', 'violet', 'purple', 'green yellow', 'snow', 'maroon1']

b0 = tk.Button(root, text = (out0), bg=random.choice(color), command=lambda:code(out0))
if b0 == 'red' :
        pin += str(value)
        e.insert('end', value)

1 Answers1

3

if b0 == 'red': compares the button b0 to the string 'red'. If you think about it, it's clear that a button will never equal a string.

You probably want to get the current background color of the button. Since bg is a config option, you can use the cget method to read its current value:

if b0.cget('bg') == 'red':

By the way, if you'd want to change the bg config value on an already existing widget, you could use the config method: b0.config(bg='red'). For more info about configuration in Tkinter, see this page.

Note that for convenience, Tkinter widgets also implement a partial dictionary interface, so you can also use b0['bg'] to read or write the value.

CherryDT
  • 25,571
  • 5
  • 49
  • 74