-1

I want them to be dark grey and have tried different colours but none are working it just displays all of them as light grey. here's my code:

from tkinter import *

cal = Tk()
cal.title("CALCULATOR")
operator=""
text_Input =StringVar()

txtDisplay= Entry(cal,font=('arial',20,'bold'), textvariable=text_Input, bd=30,insertwidth=4,
                  bg="purple", justify='right').grid(columnspan=4)

btn7=Button(cal,padx=16,bd=8, fg="black",font=('aerial', 20,'bold'),
            text="7", bg="grey").grid(row=1,column=0)

btn8=Button(cal,padx=16,bd=8, fg="black",font=('aerial', 20,'bold'),
            text="8", bg="grey").grid(row=1,column=1)

btn9=Button(cal,padx=16,bd=8, fg="black",font=('aerial', 20,'bold'),
            text="9", bg="grey").grid(row=1,column=2)

Addition=Button(cal,padx=16,bd=8, fg="black",font=('aerial', 20,'bold'),
                text="+", bg="orange").grid(row=1,column=3)

cal.mainloop()

enter image description here

martineau
  • 119,623
  • 25
  • 170
  • 301

3 Answers3

0

Changing the background color of the buttons should do, for example, the following code for the btn7, could be

btn7=Button(cal,padx=16,bd=8, fg="black",font=('aerial', 20,'bold'),
            text="7", bg="gray25").grid(row=1,column=0)

which would make it a dark shade of gray. Here is a table of all the colors in tkinker: http://www.science.smith.edu/dftwiki/index.php/Color_Charts_for_TKinter

I also saw you are using mac, and found the following post, in which someone's color wasn't working on mac. Border and Background color not showing in mac [python -Tkinter]

PlainXYZ
  • 126
  • 1
  • 8
  • You can't change the background or border of buttons on OSX due to platform constraints. –  Jun 06 '21 at 15:01
0

To get a different shade of grey, I suggest explicitly specifying the color RGB components of the color you want, that way you can have any one you want instead of being restricted to those to which tkinter has given names. You can do this in tkinter using them as hexidecimal digits in a specially formatted string (here's some documentation).

I've demonstrate this in the code below and also fixed another problem I noticed with how you're calling the grid() geometry manager. It always returns None, so your code is setting all widget variables to that value not to the widget instances like you think. See Tkinter: AttributeError: NoneType object has no attribute .

To avoid that problem you need to call it separately after creating the widgets as also shown.

On a final note: I strongly suggest you read and begin following the PEP 8 - Style Guide for Python Code to make your code more readable and easier to maintain.

from tkinter import *


BTN_COLOR = '#404040'  # Dark grey.

cal = Tk()
cal.title("CALCULATOR")
operator = " "
text_Input = StringVar()

txtDisplay = Entry(cal, font=('arial', 20, 'bold'), textvariable=text_Input, bd=30,
                   insertwidth=4, bg="purple", justify='right')
txtDisplay.grid(columnspan=4)

btn7 = Button(cal, padx=16, bd=8, fg="black", font=('aerial', 20, 'bold'),
              text="7", bg=BTN_COLOR)
btn7.grid(row=1, column=0)

btn8 = Button(cal, padx=16, bd=8, fg="black", font=('aerial', 20, 'bold'),
              text="8", bg=BTN_COLOR)
btn8.grid(row=1, column=1)

btn9 = Button(cal, padx=16,bd=8, fg="black", font=('aerial', 20, 'bold'),
              text="9", bg=BTN_COLOR)
btn9.grid(row=1, column=2)

Addition = Button(cal, padx=16, bd=8, fg="black", font=('aerial', 20, 'bold'),
                  text="+", bg="orange")
Addition.grid(row=1, column=3)

cal.mainloop()

Screenshot showing result:

Screenshot showing dark grey buttons

martineau
  • 119,623
  • 25
  • 170
  • 301
0

I believe that the error is that "grey" isn't a colour on tkinter. The link I have added is a chart with all possible colours without hex codes.

http://www.science.smith.edu/dftwiki/index.php/Color_Charts_for_TKinter