TkInter网格功能错误消息

时间:2015-01-29 14:18:33

标签: python tkinter

我正在尝试运行以下代码:

from tkinter import *

root = Tk()

topFrame = Frame(root)
topFrame.pack(side=TOP)
leftFrame = Frame(root)
leftFrame.pack(side=LEFT)
botFrame = Frame(root)
botFrame.pack(side=BOTTOM)

button1 = Button(leftFrame, text="Button 1", fg="Black")
button2 = Button(leftFrame, text="Button 2", fg="Black")
button3 = Button(leftFrame, text="Button 3", fg="Black")

button1.grid(row=0, column=0)
button2.grid(row=1, column=0)
button3.grid(row=2, column=0)

ScaleWidget = Scale(root, from_=0, to=100)
ScaleWidget.grid(row=0, column=1)

ScaleWidget = Scale(root, from_=0, to=100, orient=HORIZONTAL)
ScaleWidget.grid(row=0, column=1)

root.mainloop()

但是我要听到以下错误消息:

D:\Python\python.exe D:/untitled/TkInter_Frame_Full_GUI.py
Traceback (most recent call last):
  File "D:/untitled/TkInter_Frame_Full_GUI.py", line 21, in <module>
    ScaleWidget.grid(row=0, column=1)
  File "D:\Python\lib\tkinter\__init__.py", line 2057, in grid_configure
    + self._options(cnf, kw))
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack

并且我不确定该怎么做,非常感谢帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

您不应该为共享一个共同父级的窗口混合网格和包管理器:

  

警告:切勿在同一主窗口中混合网格和打包。 Tkinter的   我将很乐意度过余生,努力谈判   两位经理都满意的解决方案。而不是等待,杀死   应用程序,再看看你的代码。一个常见的错误   是为某些小部件使用错误的父级。

来源:http://effbot.org/tkinterbook/grid.htm