在 tkinter 中看不到框架 python

Cannot see frame in tkinter python

嘿,我正在尝试使用 tkinter 制作一个图形用户界面,以下是 me.The 使用的代码问题是没有错误,程序执行了,我会看到根目录,但没有frame.before 添加按钮我能看到框架,但添加按钮后框架消失了,我只能看到按钮请帮助

    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()

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.

你的框架也有同样的表现。如果框架不包含任何内容,它的宽度和高度将为 0,这就是您看不到它的原因。

一种解决方法是使用 pack_propagategrid_propagate,这取决于您打算在此框架中使用什么。

另一种不寻常的方法是在该框架中创建第二个框架并为其指定宽度和高度,外部框架将检查内部框架的宽度和高度并坚持下去。 See

可以有其他方法,例如几何管理器 pack 选项 fillexpand