【问题标题】:python3 tkinter grid and pack, inline packing syntax & elegancepython3 tkinter 网格和包,内联打包语法和优雅
【发布时间】:2015-02-17 05:30:00
【问题描述】:

将帧“行内”打包与 =' 将其放入变量并在下一行 .pack' 之间有什么区别?

所以我看到了如何同时进行网格和打包(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...

梦中那么多梦太不稳定了!

【问题讨论】:

    标签: python python-3.x syntax tkinter pack


    【解决方案1】:

    在第二个例子中:

    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)
    

    简而言之,不推荐“在线”方法。这是 tkinter 在 python 中的新用户最常见的错误和头痛的原因之一。

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

    在您的完整代码示例中,mainFf4ff2 都是 None。因此,例如,您认为您将 mainF 框架的引用作为主框架传递给 f4 ,但实际上您正在传递 None

    【讨论】:

    • 所以如果我不需要对框架的引用(在本例中为f5),但我仍想将其打包,那么可以做一个衬里吗? (例如,Frame(mainF, bg = "yellow", height=100, width = 60).pack(side=BOTTOM,fill=NONE))...我猜在 cmets 代码中有自动换行...但你明白我的意思吗?
    猜你喜欢
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    相关资源
    最近更新 更多