填充tkinter时,框架失去了高度[英] Frame loosing height when filled tkinter

本文是小编为大家收集整理的关于填充tkinter时,框架失去了高度的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

大家好,我正在学习使用 tkinter 制作 gui,但我遇到了一些问题,但找不到答案,我有 3 帧一个标题,一个标题的阴影和一个主要内容框架,效果很好

代码:

root = Tk()
root.geometry("1080x600")
root.minsize(width=1080, height=600)
root.maxsize(width=1080, height=600)
root.title("learning ui")

headerFrame = Frame(root, height=50, bg="#17181b")
headerShadow = Frame(root, height=3, bg="#08090a")
contentFrame = Frame(root, bg="#17181b")

headerFrame.pack(side=TOP, fill=X)
headerShadow.pack(fill=X)
contentFrame.pack(fill=BOTH, expand=True)

root.mainloop()

截图:

ui screenshot但是当我将一些东西打包到标题时,它会失去它的高度.

代码:

root = Tk()
root.geometry("1080x600")
root.minsize(width=1080, height=600)
root.maxsize(width=1080, height=600)
root.title("learning ui")

headerFrame = Frame(root, height=50, bg="#17181b")
headerShadow = Frame(root, height=3, bg="#08090a")
contentFrame = Frame(root, bg="#17181b")

headerFrame.pack(side=TOP, fill=X)
headerShadow.pack(fill=X)
contentFrame.pack(fill=BOTH, expand=True)

main.e = Entry(headerFrame)
main.e.pack(side=RIGHT)
main.e.focus_set()

searchBtn = Button(headerFrame, text="Search", command=lambda: callback(retrieve_input()))
searchBtn.pack(side=RIGHT)

def callback(q):
    print q

root.mainloop()

截图:

screenshot 2

推荐答案

所有小部件都旨在"缩小以适应"它们的子级.虽然一开始这似乎违反直觉,但它是进行小部件布局的最佳方式.不要试图强制框架或窗口为特定大小,只需将小部件放入您想要的大小即可.

如果您想关闭此功能,您可以,但我强烈建议您不要这样做.如果您想了解更多信息,请搜索"overrideredirect".

本文地址:https://www.itbaoku.cn/post/2091184.html

问题描述

Hello everyone i am learning to make a gui with tkinter but i run into something and can't find the answer to it i have 3 frames a header a shadow for the header and a main content frame and that works fine

code :

root = Tk()
root.geometry("1080x600")
root.minsize(width=1080, height=600)
root.maxsize(width=1080, height=600)
root.title("learning ui")

headerFrame = Frame(root, height=50, bg="#17181b")
headerShadow = Frame(root, height=3, bg="#08090a")
contentFrame = Frame(root, bg="#17181b")

headerFrame.pack(side=TOP, fill=X)
headerShadow.pack(fill=X)
contentFrame.pack(fill=BOTH, expand=True)

root.mainloop()

Screen shot :

ui screenshot But when i pack something to the header it looses its height.

code:

root = Tk()
root.geometry("1080x600")
root.minsize(width=1080, height=600)
root.maxsize(width=1080, height=600)
root.title("learning ui")

headerFrame = Frame(root, height=50, bg="#17181b")
headerShadow = Frame(root, height=3, bg="#08090a")
contentFrame = Frame(root, bg="#17181b")

headerFrame.pack(side=TOP, fill=X)
headerShadow.pack(fill=X)
contentFrame.pack(fill=BOTH, expand=True)

main.e = Entry(headerFrame)
main.e.pack(side=RIGHT)
main.e.focus_set()

searchBtn = Button(headerFrame, text="Search", command=lambda: callback(retrieve_input()))
searchBtn.pack(side=RIGHT)

def callback(q):
    print q

root.mainloop()

Screen shot:

screenshot 2

推荐答案

All widgets are designed to "shrink to fit" their children. While this may seem counter-intuitive at first, it's the best way to do widget layout. Don't try to force a frame or window to be a specific size, just put the widgets in that you want and it will end up being the right size.

If you want to turn this feature off you can, but I strongly recommend against it. If you want to know more, search for "overrideredirect".