首页 文章

使用Grid和Pack with Frames Tkinter

提问于
浏览
1

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

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()

因为我想要按钮之间的间距,因此它使用不同的列 . 是否有可能做到这一点?

1 回答

  • 1

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

    我认为你误解的是:每个有网格管理子节点的小部件都有自己的“网格” . 此网格无法保证与应用程序中可能使用的任何其他网格对齐 . 没有通用的列或行集 . 帧A中的列1很可能位于帧B中第10列的右侧,帧A中的第1行可能位于帧B中的第1行之下 .

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

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

    当您尝试解决布局问题时,为您的某些小部件提供独特的背景颜色确实很有帮助,这样它们就能脱颖而出 . 例如,如果给 buttonsFrame 粉红色背景,则填充整个窗口宽度 .

相关问题