Search code examples
pythonuser-interfacebuttontkinterframe

Cannot see frame in tkinter python


hey there i was trying to make a gui using tkinter the following is the code that is used by me.The problem is there are no errors and the program execute and i will see the root but there is no frame.before adding button i was able to see frame but after adding button the frame disappears and i can only see button please help

    from tkinter import*
    from tkinter import ttk
    root=Tk()
    root.title("STUDY")
    style=ttk.Style() 
    style.configure("custom.TFrame",background="black")
    frame=ttk.Frame(root,style="custom.TFrame")
    frame.pack()
    frame.config(height=100,width=100)
    ttk.Button(frame,text="CLICK ME").pack()
    root.mainloop()

Solution

  • The geometry management of tkinter is characterized by this Quote here:

    By default a top-level window appears on the screen in its natural size, which is the one determined internally by its widgets and geometry managers.

    Your frame behave in the same way. If the frame contains nothing it get a width and height of 0, thats why you cant see it.

    A way arround is to use either pack_propagate or grid_propagate it depends on what you intend to use in this frame.

    Another unusally way would be to create a second frame in that frame and give it a width an height, the outerframe will check the width and height of the inner frame and stick to it. See

    there can be additional ways like for the geometry manager pack the options fill and expand.

    For more