Change fill color of Label widget in ttk / python

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 a lot of white space.

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

Now I tried to change the foreground and background shortcut to no avail. The fill area remains gray. Then I put the label in the Frame widget and tried to change the color of the foreground / background frame. But, unfortunately, the Frame widget does not listen to the width = and height = values. Also, if I change the foreground color, the SUNKEN border will change color - really cool, but completely useless to me: /.

Can someone help me with this? The current idle code is as follows:

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) 
+4
source share
1 answer

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

Width and height do not work, because both grid and pack default to “shrink to fit” for the widget containing the widget. This is called geometry propagation. You can enable or disable this function using the grid_propagate or pack_propagate methods in the containing widget.

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

+3
source

Source: https://habr.com/ru/post/1391635/


All Articles