在同一个GUI中使用GRID和PACK

2024-04-24 11:46:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在四处寻找,很多人说这是可能的,但没有如何做到这一点的例子。你知道吗

我希望在同一个GUI中同时使用pack和grid。我已经设置了2个不同的帧1使用pack-the-other-grid,但是在尝试运行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 (most recent call last): File "C:/Users/Ganjena/Desktop/Course/Projects/helloworld/test.py", line 22, in bottomFrame.grid() File "C:\Users\Ganjena\AppData\Local\Programs\Python\Python36-32\lib\tkinter__init__.py", line 2220, in grid_configure + self._options(cnf, kw)) _tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack

Process finished with exit code 1


Tags: falsetkintertop错误guirootwidthframe
1条回答
网友
1楼 · 发布于 2024-04-24 11:46:43

topFramebottomFrame都是根窗口的子窗口,但是您在一个窗口上调用了.pack(),在另一个窗口上调用了.grid()。换句话说,错误消息的意思就是它所说的!你知道吗

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

相关问题 更多 >