0

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()
Sandyagu R
  • 45
  • 3

1 Answers1

0

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

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • i added root.geometry("100x100") but the problem still exist. – Sandyagu R Oct 09 '20 at 15:59
  • the problem is when i add a button before that i could see but after adding button i cannot see it – Sandyagu R Oct 09 '20 at 16:07
  • add `frame.pack_propagata(0)` below `frame.pack()` and see what happens – Thingamabobs Oct 09 '20 at 17:26
  • Now it works thanks for the help.can you please explain me why this happens in windows because i am taking a course on python and i followed exactly the same code my mentor used but he doesnt face any issues he is using it in mac.is this a problem only with windows.thanks again – Sandyagu R Oct 09 '20 at 17:43
  • Please read my answer carfully. As I already said, **tkinter** calculates the *natural size* of each *master* this can be a frame or a window or toplevel, it dosent matter. Also check out the link that I have posted there can be found a lot for beginners. At least https://meta.stackexchange.com/a/5235/828183 – Thingamabobs Oct 09 '20 at 17:48
  • @SandyaguR I added an additional way, maybe he was using these options. – Thingamabobs Oct 09 '20 at 17:51