python3 tkinter grid and pack, inline packing syntax & elegance

本小妞迷上赌 提交于 2021-02-05 08:07:03

问题


What's the difference between packing a frame "in line" versus ='ing it to a variable and .pack'ing it on the next line?

So I saw how to do grid and pack at the same time (see here), but after playing around with it, I ran into something weird. If you change line 16/17 from:

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

to:

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

At the end of the code where the buttons get put into the grid within a pack within a frame within a frame within a dream... My once error-free code now gives the error:

TclError: cannot use geometry manager grid inside .164287488 which already has slaves managed by pack

How? Wha-huh? Why?

My full code here:

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()

I'm using Spyder2 within ipython within anaconda 64 within python 3.4.1 64 within Windows 7 64...

That many dreams within dreams is too unstable!


回答1:


In the second example:

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

f5 is None. This is not the case in the first example:

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

In short terms, the "in line" method is not-recommended. It is one of the most common mistakes and causes of headache for new users of tkinter in python.

The reason for None is very simple: pack() and grid() return None.

In your full code example, mainF, f4, f, f2 are all None. So for exmaple, you think you are passing a reference to mainF frame as master to f4 , but in fact you are passing None.



来源:https://stackoverflow.com/questions/28555295/python3-tkinter-grid-and-pack-inline-packing-syntax-elegance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!