0

This here is my code:

for i in range(0, len(btn_txt)):
    btns.append(tk.Button(calc, text = btn_txt[i], background = "#CCCCCC"))
    btns[i].grid(column = i % 7, row = i // 7 + 1)
    btns[i].bind("<Button-1>", lambda event, n = btn_txt[i]: get_key(n))

Problem: background = "#CCCCCC" does not change the color of my buttons... I tried looking into the docs of tkinter and did further search but i did not find a solution for it. I hope someone can help.

PS: I am coding on mac

Thanks in advance for your help!

GhostBotBoy
  • 21
  • 1
  • 3

1 Answers1

-1

Your code does work, it's just that '#CCCCCC' is a very similar color to that of default. So it's hard to tell the difference.

If you color the text instead of the background for example, with foreground option, it makes the text 'invisible':

import tkinter as tk

root = tk.Tk()

btn = tk.Button(root, text="asd")

btn['fg'] = '#CCCCCC'

btn.pack()

root.mainloop()

Using this answer one can inspect RGB values of colors let's see what they yield:

import tkinter as tk

root = tk.Tk()

btn = tk.Button(root, text="asd")

default_rgb = btn.winfo_rgb(btn['bg'])
wanted_rgb = btn.winfo_rgb('#CCCCCC')
reference_rgb = btn.winfo_rgb('grey')

print(default_rgb)
print(wanted_rgb)
print(reference_rgb)

btn.pack()

root.mainloop()

As you can see the two colors, the default SystemButtonFace and #CCCCCC are very similar especially when you think of how close of a reference grey is and how far its rgb values are.


See below example for an easy to tell coloring:

import tkinter as tk

root = tk.Tk()

btn = tk.Button(root, text="Colors")

#when button isn't pressed
btn['fg'] = 'orange'
btn['bg'] = 'blue'

#when button is pressed
btn['activeforeground'] = 'red'
btn['activebackground'] = 'green'

btn.pack()

root.mainloop()
Nae
  • 14,209
  • 7
  • 52
  • 79