简体   繁体   English

Tkinter框架和网格

[英]Tkinter Frame and Grid

For the life of me, I cannot understand grid within Frame . 为了我的一生,我无法理解Frame grid I'm trying to create the below configuration, but I'm getting something different (highlighted area is the troublesome part). 我正在尝试创建以下配置,但是却有所不同(突出显示的区域是麻烦的部分)。

Here is the code : 这是code

from tkinter import *
root = Tk()

weather_root = Frame(root,width=1000, height=5, bg = 'white')
weather_root.pack(side=TOP)

quote_root = Frame(root,width=1000, height =5, bg = 'white')
quote_root.pack(side=TOP)

news_root = Frame(root,width=1000, height =100, bg = 'white')
news_root.pack(side=TOP,  fill= BOTH)

financial_root= Frame(root,width=1000, height =100, bg = 'white')
financial_root.pack(side=TOP,  fill= BOTH)

# PROBLEM BOX
time_root = Frame(root, bg = 'yellow')
time_root.pack(side = RIGHT,  fill= BOTH)

I'm very new to this still, so I'm sure it's something obvious, but what is it? 我对此还是很陌生,所以我敢肯定这很明显,但这是什么? (In the picture I have it split as two frames - that's the ultimate goal, but in the near term, I'm just trying to get the frame to show up against the right of the current placed frames). (在图片中,我将其拆分为两个帧-这是最终目标,但在短期内,我只是试图使该帧显示在当前放置的帧的右侧)。 Thanks very much! 非常感谢!

The expected output: 预期输出:

在此处输入图片说明

The actual output: 实际输出:

在此处输入图片说明

The easiest way to accomplish the desired output would be to create two separate sub frames. 完成所需输出的最简单方法是创建两个单独的子帧。 You can pack the weather, quote, news and financial root frames into the left subframe, then pack the time frame into a right subframe. 您可以将天气,报价,新闻和财务根目录框架打包到左侧子框架中,然后将时间框架打包到右侧子框架中。 Last, you would pack them both into root, one using SIDE=LEFT and the other using SIDE=RIGHT. 最后,您将它们都打包到根目录中,一个使用SIDE = LEFT,另一个使用SIDE = RIGHT。 Additionally, it is possible to use Grid and Pack effectively within one app, but one individual widget (frame, for example) can only be managed using one layout manager (grid vs pack) at a time. 此外,可以在一个应用程序中有效地使用“网格和打包”,但是一次只能使用一个布局管理器(网格与打包)来管理一个单独的小部件(例如框架)。 So you can grid widgets such as frames into a frame, then pack that frame into another frame. 因此,您可以将诸如框架之类的窗口小部件网格化为一个框架,然后将该框架打包为另一个框架。 Or, you could pack things into a subframe, then grid it into the main frame of the window. 或者,您可以将东西包装到子框架中,然后将其网格化到窗口的主框架中。

The pack geometry manager is not good for laying things out in a grid. pack几何图形管理器不适用于将内容放置在网格中。 Unsurprisingly, grid is the better choice. 毫不奇怪, grid是更好的选择。 It is going to be very difficult to do what you want with pack unless you add additional frames specifically to aid in layout. 除非您添加专门用于辅助布局的其他框架,否则要对pack进行任何操作都将非常困难。

Doing this with grid is very straight-forward. 使用grid操作非常简单。 It would look something like this: 它看起来像这样:

weather_root.grid(  row=0, column=0, sticky="nsew")
quote_root.grid(    row=1, column=0, sticky="nsew")
news_root.grid(     row=2, column=0, sticky="nsew")
financial_root.grid(row=3, column=0, sticky="nsew")
time_root.grid(     row=0, column=1, sticky="nsew", rowspan=4)

You would also need to use root.grid_rowconfigure and root.grid_columnconfigure to apply weights to the rows and columns that should grow or shrink when the window is resized. 您还需要使用root.grid_rowconfigureroot.grid_columnconfigure将权重应用于在调整窗口大小时应增加或缩小的行和列。

If you want to use pack , I recommend adding two extra frames: one for the left (gray), and one for the right (yellow). 如果要使用pack ,我建议添加两个额外的框架:一个用于左侧(灰色),另一个用于右侧(黄色)。 You can use pack for those two. 您可以为这两个使用pack Then, on the left you could use pack to stack the areas top-to-bottom. 然后,在左侧您可以使用pack堆叠从上到下的区域。 Whether that's the right solution in your specific case, I don't know. 我不知道这是否是您特定情况下的正确解决方案。


Notes: 笔记:

  1. I strongly recommend grouping your calls grid or pack in this way. 我强烈建议您以这种方式将呼叫grid分组或pack It's much easier to manage when they are all in one spot rather than interleaved with the other code. 当它们全部集中在一个位置而不是与其他代码交错时,管理起来容易得多。

  2. I don't recommend using extra whitespace as showin in the example. 我不建议在示例中使用多余的空格作为showin。 I did it just to make it easier for you to see how the rows and columns relate. 我这样做只是为了让您更轻松地了解行和列之间的关系。

  3. For the canonical description of how pack works, see http://tcl.tk/man/tcl8.5/TkCmd/pack.htm#M26 有关pack如何工作的规范描述,请参见http://tcl.tk/man/tcl8.5/TkCmd/pack.htm#M26

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

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