简体   繁体   English

将Grid and Pack与Frame Tkinter一起使用

[英]Using Grid and Pack with Frames Tkinter

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? 是否有可能做到这一点?

Yes, it is possible. 对的,这是可能的。 In fact, it's the recommended way to build complex GUIs. 实际上,这是构建复杂GUI的推荐方法。

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. 框架A中的第1列很可能在框架B中的第10列的右边,而框架A中的第1行可能在框架B的第1行之下。

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. 它将尝试尽可能小,并位于其父级的中上部分。

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. 该框架的第0列的宽度为零,因为它为空,第1列的宽度为开始按钮,第2列的宽度为零,因为它为空,而第3列的宽​​度为停止按钮。 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); ) 如果要在它们之间留出空间,一个简单的解决方案是将第2列强制为特定大小(例如: buttonsFrame.grid_columnconfigure(2, minsize=100);

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. 例如,如果给buttonsFrame设置粉红色背景,您将很快看到它没有填充窗口的整个宽度。

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

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