如何让.grid_columnconfigure()在Frame内工作? [英] How to get .grid_columnconfigure() working inside Frame?

查看:331
本文介绍了如何让.grid_columnconfigure()在Frame内工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用grid-Method和grid_columnconfigure/grid_rowconfigure在python中使用tkinter创建一个GUI. 不幸的是,这在框架内不起作用. 我如何获得这项工作?

I want to create a GUI with tkinter in python using grid-Method and grid_columnconfigure/grid_rowconfigure. Unfortunately, this is not working inside a Frame. How can I get this work?

from tkinter import *

master = Tk()
master.state('zoomed')
f = Frame(master, width=800, height=400)

Label1 = Label(f, text='Label 1')
Label2 = Label(f, text='Label 2')

f.grid_columnconfigure(0, weight=1)
f.grid_columnconfigure(2, weight=1)
f.grid_columnconfigure(4, weight=1)

Label1.grid(row=0, column=1)
Label2.grid(row=0, column=3)

f.pack()

master.mainloop()

附加问题: 我得到了很好的答案,所有的pack-Manager都可以正常工作. 但是,如果使用grid-Manager怎么办呢?

ADDITIONAL QUESTION: I got great answers, all is working fine with pack-Manager. But how could I do this if using grid-Manager?

推荐答案

grid_columnconfigure正常运行.问题是您的框架默认情况下会将其尺寸设置为可能的最小尺寸以适合标签.由于空列没有大小,因此框架的宽度将足以容纳两个标签.

The grid_columnconfigure is working fine. The problem is that your frame will by default set its size to the smallest possible size to fit the labels. Since empty columns don't have a size, the frame will be just wide enough to hold the two labels.

如果在开发过程中为frame提供独特的颜色,这将很容易实现.有时还有助于为框架提供视觉边框,以便您可以看到其边界.

This will be easy to visualize if you give frame a distinctive color during development. It also sometimes helps to give the frame a visual border so you can see its boundaries.

虽然我不知道您的最终目标是什么,但是如果框架填满了整个窗口,则可以看到列之间的空格:

While I don't know what your ultimate goal is, you can see the spaces between the column if you have the frame fill the entire window:

f.pack(fill="both", expand=True)

如果要使用grid而不是pack,则必须做更多的工作.简而言之,将框架放在第0行第0列中,并对该行和列赋予非零权重,以便网格将为该行和列分配所有未使用的空间.

If you want to use grid instead of pack, you have to do a bit more work. In short, put the frame in row 0 column 0, and give that row and column a non-zero weight so that grid will give all unused space to that row and column.

f.grid(row=0, column=0, sticky="nsew")
master.grid_rowconfigure(0, weight=1)
master.grid_columnconfigure(0, weight=1)

如果要强制将窗口设置为特定大小,则可以使用主窗口的geometry方法:

If you want to force the window to be a specific size, you can use the geometry method of the master window:

master.geometry("800x400")

这篇关于如何让.grid_columnconfigure()在Frame内工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆