python3 tkinter 网格和打包,内联打包语法和优雅

标签 python python-3.x syntax tkinter pack

“在线”打包帧与 ='ing 到变量并在下一行 .pack'ing 之间有什么区别?

所以我看到了如何同时进行网格和打包( see here ),但是在尝试之后,我遇到了一些奇怪的事情。如果您将第 16/17 行更改为:

f5 = Frame(mainF, bg = "yellow", height=100, width = 60)
f5.pack(side=BOTTOM,fill=NONE)

至:

f5 = Frame(mainF, bg = "yellow", height=100, width = 60).pack(side=BOTTOM,fill=NONE)

在代码的末尾,按钮被放入梦想中的框架内的框架内的包内的网格中......我曾经没有错误的代码现在给出了错误:

TclError: cannot use geometry manager grid inside .164287488 which already has slaves managed by pack
怎么办?哇哈?为什么?

我的完整代码在这里:

from tkinter import Tk, Frame, Label, Entry, LEFT, TOP, YES, NONE, BOTH, RIGHT, BOTTOM,SE, Button,W,E,N,S

root = Tk()

mainF = Frame(root, bg = "green", height=100, width = 1060).pack(side=BOTTOM,fill=NONE)

f4 = Frame(mainF, bg = "blue", width = 300).pack(side=BOTTOM,fill=BOTH)

f = Frame(f4, bg = "orange", width = 500, height = 500).pack(side=LEFT, expand = 1)

f3 = Frame(f, bg = "red", width = 500)
f3.pack(side=LEFT, expand = 1, pady = 10, padx = 50)

f2 = Frame(f4, bg = "black", height=100, width = 100).pack(side=LEFT, fill = BOTH)

f5 = Frame(mainF, bg = "yellow", height=100, width = 60)
f5.pack(side=BOTTOM,fill=NONE)
#f7 = Frame(f5, bg = "green", height=100, width = 160).pack(side=BOTTOM,fill=BOTH)
#f6 = Frame(f7, bg = "green", height=100, width = 360).pack(side=BOTTOM,fill=BOTH)

b = Button(f2, text = "test")
b.pack()

Button(f3, text = "1").grid(row=0, column=0)
Button(f3, text = "2").grid(row=1, column=1)
Button(f3, text = "3").grid(row=2, column=2)

Button(f5, text = "1").grid(row=0, column=0)
Button(f5, text = "2").grid(row=1, column=1)
Button(f5, text = "3").grid(row=2, column=2)

root.mainloop()

我在 Windows 7 64 中的 python 3.4.1 64 中的 anaconda 64 中的 ipython 中使用 Spyder2...

That many dreams within dreams is too unstable!

最佳答案

在第二个示例中:

f5 = Frame(mainF, bg = "yellow", height=100, width = 60).pack(side=BOTTOM,fill=NONE)

f5 为无。第一个示例中的情况并非如此:

f5 = Frame(mainF, bg = "yellow", height=100, width = 60)
f5.pack(side=BOTTOM,fill=NONE)

简而言之,不推荐“排队”方法。这是Python中最常见的错误之一,也是令tkinter新用户头疼的原因之一。

None 的原因非常简单:pack()grid() 返回 None。

在完整的代码示例中,mainFf4ff2 均为 None。因此,对于 exmaple,您认为您正在将 mainF 框架的引用作为 master 传递给 f4 ,但实际上您正在传递 None

关于python3 tkinter 网格和打包,内联打包语法和优雅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28555295/

相关文章:

python - 如何使用 blockblobservice 的 delete_blob 方法删除 azure 容器内的文件夹(blob)?

自减/自增运算符的Java表达式解释规则

Python 3如何将以字节表示的大量数字转换为整数?

python - django-rest-framework:无法调用 `.is_valid()`,因为在实例化序列化程序实例时没有传递 `data=` 关键字参数

python - 使用 Salt 的 virtualenv.managed 或 pip.installed 设置 python 虚拟环境?

python - 在python中用逗号运算符分隔的行中查找字谜单词

python - 这是我从python multiprocess可以得到的最多的东西吗?

c# - C#中的花括号

C:int 类型存储为 long long 类型时,技术上会发生什么?

python - 从一个列表中删除另一个列表的元素,同时保留重复项