4

I am trying to display an image with ttk/tkinter in python. The image has a white border, and I want to display this image on a larger white background - so it has lots of white space around.

For this I use "padx" and "pady" in the label with 100px each. Unfortunately the padding area is gray.

Now I tried changing the label's foreground and background color to no avail. The padding area stays gray. Then I put the label in a Frame widget, and tried changing the Frame's foreground/background color. But unfortunately the Frame widget does not listen to width= and height= arguments. Also if I change the foreground color the SUNKEN border changes color - really cool, but totally useless for me :/ .

Could anybody help me with this? The current nonworking code looks like this:

style = Style()

style.configure('Dlr.TFrame', background="blue", relief=SUNKEN)
frm = Frame(self, style="Dlr.TFrame") # does not work: ,height=500,width=500)
frm.grid(row=0, column=0, rowspan=8, columnspan=2, sticky=N+S+E+W)

style.configure("Dlr.TLabel", background="white")
style.configure("Dlr.TLabel.padding", background="white") # just guessed ...
self.IMG = Label(frm, style="Dlr.TLabel")
self.IMG.grid(row=0, column=0, padx=100, pady=100)
Axel
  • 41
  • 1
  • 2

1 Answers1

3

Your technique for putting the image inside a frame and then setting the frame color is the right technique.

The width and height don't work because both grid and pack cause the containing widget to "shrink-to-fit" by default. This is called geometry propagation. You can turn this feature on or off using the methods grid_propagate or pack_propagate on the containing widget.

For example, if you call frm.grid_propagate(False) and then set the widget and height of frm, the width and height will be honored.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks for your response. I've got mixed results with this - or gGood news and bad news. Good news is that I made the frame grow to a fixed size with `grid_propagate(false)`. Bad news is that the background is still gray, _and_ now the image is in the top-left corner of the frame. New code: `style.configure('Dlr.TFrame', relief=RAISED, bg="white") # bg does not work frm = Frame(self, style="Dlr.TFrame", width=500, height=500) frm.grid_propagate(False) frm.grid(row=0, column=0, rowspan=10, columnspan=2) # no changes on image/label, just removed the padx/y` – Axel Jan 18 '12 at 19:06
  • you have to use the full keyword "background" not just "bg" as you posted above. – timeyyy Apr 25 '15 at 10:46