简体   繁体   English

TkInter:无法用其他小部件填充框架

[英]TkInter: Can't fill a Frame with another widget

First time with TkInter. 第一次使用TkInter。 According to what I've read, you can use Frame widgets to hold other widgets. 根据我所读的内容,您可以使用Frame小部件来容纳其他小部件。 So, I laid down some preview Frames as such: 因此,我放下了一些预览框架,例如:

import tkinter as tk


root = tk.Tk()

TextFrame = tk.Frame(root, width=200, height=600, bg="red")
TextFrame.pack(side="left")

ListFrame = tk.Frame(root, width=800, height=600, bg="blue")
ListFrame.pack(side="right")

However, when I add a widget and tell it to fill the Frame, it simply replaces the frame instead of expanding to fill the area. 但是,当我添加一个小部件并告诉它填充框架时,它只是替换框架而不是扩展以填充区域。 Test code used: 使用的测试代码:

ListBox = tk.Listbox(ListFrame, selectmode="single", bg="#545454", fg="#d9d9d9")
for X in range(20):
    ListBox.insert('end', X)
ListBox.pack(fill='both', expand=True)

The same test code expands properly if I set the parent to the main 'root' object. 如果将父对象设置为主要的“根”对象,则相同的测试代码可以正确扩展。 Example: 例:

import tkinter as tk


root = tk.Tk()
root.geometry("1000x600")

ListBox = tk.Listbox(root, selectmode="single", bg="#545454", fg="#d9d9d9")
for X in range(20):
    ListBox.insert('end', X)
ListBox.pack(side="right", fill='both', expand=True)

TextBox = tk.Message(root, text="A bunch of text", width=100)
TextBox.pack(side="left", fill="y")

Am I doing something wrong, here? 我在做错什么吗?

Frames are like elastics, they grip their content. 框架就像松紧带,它们抓住了它们的内容。 You could add: 您可以添加:

ListFrame.pack_propagate(False)

to switch off that behavior 关闭该行为

It's impossible to say what the right answer is since your question doesn't explain exactly the look and behavior that you're wanting to achieve. 无法说出正确的答案是正确的,因为您的问题并不能完全解释您想要获得的外观和行为。

By default, widgets shrink or expand to fit their contents. 默认情况下,小部件会收缩或扩展以适合其内容。 99.9% of the time this is exactly the right thing, and lets to UIs that are very responsive to the change (different fonts, different resolutions, different window sizes, etc.). 99.9%的时间是完全正确的事情,并且可以让UI对更改做出快速响应(不同的字体,不同的分辨率,不同的窗口大小等)。 In your case, the listbox doesn't replace the frame. 在您的情况下,列表框不会替换框架。 Instead, the frame is simply shrinking to fit the listbox. 相反,框架只是在缩小以适合列表框。

It is doing this because you didn't tell it to do anything different. 这样做是因为您没有告诉它做任何不同的事情。 When you placed the frame in the root window, you didn't tell tkinter that you wanted the frame to fill the window. 当您将框架放置在根窗口中时,您没有告诉tkinter您希望框架填满窗口。 You've just sort-of left it to float at the top, which gives it the freedom to grow or shrink to fit its contents. 您只是让它浮动在顶部,这使其可以自由缩放以适应其内容。

By giving proper attributes to TextFrame.pack(side="left") you can have that frame fill the window, and therefore prevent it from shrinking to fit its children. 通过为TextFrame.pack(side="left")赋予适当的属性,可以使该框架填充窗口,从而防止其缩小以适合其子级。

TextFrame.pack(side="left". fill="both", expand=True)

Of course, that assumes you want the frame (and thus, the text widget it contains) to fill the window. 当然,这假定您希望框架(因此,其中包含的文本小部件)填充窗口。 If you are putting other windows around it, that may change which options you want to use. 如果您要放置其他窗口,则可能会更改您要使用的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM