【问题标题】:Using GRID and PACK in the same GUI在同一个 GUI 中使用 GRID 和 PACK
【发布时间】:2017-03-30 22:41:03
【问题描述】:

我一直在闲逛,很多人说这是可能的,但没有关于如何做到这一点的示例。

我希望在同一个 GUI 中同时使用 pack 和 grid。我使用打包其他网格设置了 2 个不同的帧 1,但在尝试运行 GUI 时仍然出现错误。

我的印象是你可以使用不同的命令,只要有不同的帧?

这是我的代码

from tkinter import*

root = Tk()
root.title("UKIND Industry Tool")
root.geometry("600x600")
root.resizable(width=False, height=False)

# ----- Top Frame -----
topFrame = Frame(root, bg="grey", width=600, height=25, pady=1)
topFrame.pack(side=TOP, fill=X)

# ----- Top Frame Label -----

oreCalc = Button(topFrame, text= "Ore Calculator")
minCalc = Button(topFrame, text= "Mineral Calculator")
oreCalc.pack(side=LEFT)
#minCalc.pack(side=LEFT)

# ----- Bottom Frame -----

bottomFrame = Frame(root, bg="green", width=600, height=585, pady=1)
bottomFrame.grid()


root.mainloop()

这是回溯错误:

Traceback(最近一次调用最后一次): 文件“C:/Users/Ganjena/Desktop/Course/Projects/helloworld/test.py”,第 22 行,在 bottomFrame.grid() 文件“C:\Users\Ganjena\AppData\Local\Programs\Python\Python36-32\lib\tkinter__init__.py”,第 2220 行,在 grid_configure + self._options(cnf, kw)) _tkinter.TclError: 不能在内部使用几何管理器网格。已经有由 pack 管理的 slave

进程以退出代码 1 结束

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    topFramebottomFrame 都是您的根窗口的子窗口,但您在其中一个上调用了 .pack(),在另一个上调用了 .grid()。换句话说,错误消息的含义正是它所说的!

    您实际上可以做的是在bottomFrame 的孩子上使用.grid();这与在topFrame 的孩子或bottomFrame 本身上使用.pack() 不冲突。但是任何两个共享父级的小部件都必须共享一个几何管理器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-23
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      相关资源
      最近更新 更多