0
import tkinter
from tkinter import ttk

class GUI(tkinter.Tk):
    def __init__(self):
        tkinter.Tk.__init__(self)
        self.geometry("500x500")
        self.title("Red Frame Example")
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        red_frame_style = ttk.Style()
        red_frame_style.configure("RedFrame.TFrame", background = "red") 
        
        red_frame = ttk.Frame(self, style="RedFrame.TFrame", width="250", height="250")
        red_frame.grid_rowconfigure(0, weight=1)
        red_frame.grid_columnconfigure(0, weight=1)
        red_frame.grid(row=0, column=0, sticky="n")
        
        button = tkinter.Button(red_frame, text='Stop', width=25, command=self.destroy)
        button.grid(row = 1, column = 0)
        
gui = GUI()
gui.mainloop()   

I am trying to get a 250 by 250 red frame to show up on my window in tkinter. Answers to this question suggested splitting a line like red_frame = tkinter.Frame(self, style="RedFrame.TFrame", width="250", height="250").grid(row=0, column=0, sticky="n") into 2 different lines, which is what I tried to do in my code below, but the red frame still did not appear. What am I doing wrong here, and how can I fix it?

noG23
  • 93
  • 2
  • 7
  • You will have to add `red_frame.grid_propagate(False)`, if you want to see the entire region – Delrius Euphoria Dec 23 '21 at 18:05
  • @CoolCloud I added that exact line and now the red frame does show up on the window. Thanks so much! – noG23 Dec 23 '21 at 18:09
  • The red frame always shows up on the window, but it just shows the part in which it is occupied – Delrius Euphoria Dec 23 '21 at 18:11
  • Why do you care that the frame is exactly 250x250? Usually it's better to let tkinter grow or shrink widgets to fit. – Bryan Oakley Dec 23 '21 at 18:26
  • @BryanOakley If I remove the width and height, then the red frame doesn't show up at all. Is this not supposed to happen? – noG23 Dec 23 '21 at 21:18
  • If you do not specify the width and height and call `red_frame.grid_propagate(0)`, then the size of the frame will be 1x1. – acw1668 Dec 24 '21 at 00:18
  • @acw1668 So how do I let tkinter control the size of the frame, as Bryan mentioned? I tried changing the integer in propagate to be higher than 0 but nothing seems to happen. – noG23 Dec 24 '21 at 00:35
  • Remove `red_frame.grid_propagate(...)`. Then the size of the frame will be the same as the button so you don't see any red part. – acw1668 Dec 24 '21 at 00:59

0 Answers0