Tkinter GUI Python background color

21,356

If the issue is that you don't like the default button effect on your system, you don't have to change the border width; instead you can set relief='flat' in the button declaration. That way, you'll still get the "sunken" look when you click the button, which you won't get if you just set borderwidth to 0 or a value close to it. Another issue with lessening borderwidth is that it may make the button smaller than expected.

Share:
21,356

Related videos on Youtube

Jared
Author by

Jared

Updated on October 19, 2020

Comments

  • Jared
    Jared over 3 years

    I'm writing a Tkinter application with buttons, graphs, sliders, etc, but I can't get their background color to be uniform.

    import Tkinter
    from Tkinter import *
    root = Tk()
    root.title('Button')
    root.configure(bg='gray')
    
    Button(text='Button', bg='gray').pack(side=BOTTOM)
    root.mainloop()
    

    If you run this code, the background of the main window is indeed gray, but the image of the button has a white area around it. Is there a way to fix this?

    • mgilson
      mgilson over 11 years
      does borderwidth=0 help at all?
    • JAB
      JAB over 11 years
      I'm not sure what is causing your issue, because when I execute the code myself I don't get any "white area" around the button. I get a bit of white on the top and left, but that's due to the relief setting of the button.
    • Admin
      Admin over 8 years
      Wow, looking back at old posts and seeing people import the same thing twice in 1 script
  • pmacd
    pmacd over 11 years
    Now that I look into it more, borderwidth=.1 works equally well
  • pmacd
    pmacd over 11 years
    In addition, if you are in interested in keeping the button gray when you push it, you can use backgroundactive='gray'.
  • pmacd
    pmacd over 11 years
    Unless there is some difference in the version of Tk we are using, I definitely get a sunken effect for both alternatives here. I can see a 1-2 pixel change in the size of the button making a difference if you are trying to make callbacks based on coordinates, but I doubt you'll notice any visual difference.
  • pmacd
    pmacd over 11 years
    I see, using just relief='flat' still gives you a border around the sunken part.