在框架 Tkinter 中使用 Grid 和 Pack [英] Using Grid and Pack with Frames Tkinter

查看:27
本文介绍了在框架 Tkinter 中使用 Grid 和 Pack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在框架中使用网格然后在根窗口中打包?例如:

Is it possible to use grid in a frame and then pack in the root window? For example:

from Tkinter import *
import ttk as ttk
root = Tk()
text = Text(root)

buttonsFrame = Frame(root)
start = Button(buttonsFrame)
start["text"] = "Start"
start.grid(row = 0, column=1)
stop = Button(buttonsFrame, )
stop["text"] = "Stop"
stop.grid(row = 0, column=3)
buttonsFrame.pack(side=TOP)

tabbedPane = ttk.Notebook(root)

raw =  ttk.Frame(tabbedPane)
interpreted =  ttk.Frame(tabbedPane)
text = Text(raw)
text.pack(fill=BOTH, expand=1, side=TOP)

textInterpreted = Text(interpreted)
textInterpreted.pack(fill=BOTH, expand=1, side=TOP)

tabbedPane.add(raw, text="RAW")
tabbedPane.add(interpreted, text="Application")
tabbedPane.pack(fill=BOTH, expand=1, side=TOP)
checkBoxesFrame = Frame(root)
stkCheck = Checkbutton(checkBoxesFrame, text="STK/CAT")
stkCheck.pack(side=LEFT)
stkFile = Checkbutton(checkBoxesFrame, text="File IO")
stkFile.pack(side=LEFT)
stkAuth = Checkbutton(checkBoxesFrame, text="Auth")
stkAuth.pack(side=LEFT)
checkBoxesFrame.pack()

root.mainloop()

因为我想要按钮之间的间距,因此为什么它使用不同的列.可以这样做吗?

As I want spacing between the between the buttons hence why its using different columns. Is it possible to do this?

推荐答案

是的,这是可能的.事实上,这是构建复杂 GUI 的推荐方法.

Yes, it is possible. In fact, it's the recommended way to build complex GUIs.

我认为您的误解是:每个具有由网格管理的子项的小部件都有自己的网格".此网格不能保证与应用程序中可能使用的任何其他网格对齐.没有通用的列或行集.很有可能 A 帧中的第 1 列可能位于 B 帧中第 10 列的右侧,而 A 帧中的第 1 行可能位于 B 帧中第 1 行的下方.

I think what you are misunderstanding is this: each widget that has children managed by grid has its own "grid". This grid isn't guaranteed to line up with any other grid that might be used in the app. There is no universal set of columns or rows. It's quite possible that column 1 in frame A could be to the right of column 10 in frame B, and row 1 in frame A could be below row 1 in frame B.

在您的特定情况下,您将按钮框架打包在顶部,但由于您没有指定任何选项,它不会填充父窗口的宽度.它将尝试尽可能小,并在其父级的顶部中间部分.

In your specific case you are packing the button frame at the top, but because you didn't specify any options it will not fill the width of the parent window. It will attempt to be as small as possible, and in the top-middle section of its parent.

当您在其中放置两个小部件时,框架会扩展到足以容纳这些小部件.该框架的第 0 列的宽度为零,因为它是空的,第 1 列的宽度为开始按钮的宽度,第 2 列的宽度为零,因为它是空的,第 3 列的宽度为停止按钮的宽度.如果您想要它们之间的空间,一个简单的解决方案是强制第 2 列为特定大小(例如:buttonsFrame.grid_columnconfigure(2, minsize=100);)

When you put two widgets in there, the frame expands just big enough to contain those widgets. Column 0 of that frame will have zero width since it is empty, column 1 will have the width of the start button, column 2 will have zero width because it is empty, and column 3 will have the width of the stop button. If you want space between them, a simple solution is to force column 2 to be a specific size (eg: buttonsFrame.grid_columnconfigure(2, minsize=100);)

当您尝试解决布局问题时,确实有助于为您的一些小部件提供独特的背景颜色,使它们脱颖而出.例如,如果你给 buttonsFrame 一个粉红色的背景,你很快就会发现它没有填满窗口的整个宽度.

When you are trying to solve layout problems, it really helps to give some of your widgets distinctive background colors so they stand out. For example, if you give buttonsFrame a pink background you'll quickly see that it doesn't fill the whole width of the window.

这篇关于在框架 Tkinter 中使用 Grid 和 Pack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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