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.
Related videos on Youtube
Author by
Jared
Updated on October 19, 2020Comments
-
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 over 11 yearsdoes
borderwidth=0
help at all? -
JAB over 11 yearsI'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 over 8 yearsWow, looking back at old posts and seeing people import the same thing twice in 1 script
-
-
pmacd over 11 yearsNow that I look into it more,
borderwidth=.1
works equally well -
pmacd over 11 yearsIn addition, if you are in interested in keeping the button gray when you push it, you can use
backgroundactive='gray'
. -
pmacd over 11 yearsUnless 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 over 11 yearsI see, using just
relief='flat'
still gives you a border around the sunken part.