python3 tkinter grid and pack, inline packing syntax & elegance

后端 未结 1 2009
春和景丽
春和景丽 2021-01-27 00:58

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 sa

1条回答
  •  天涯浪人
    2021-01-27 01:30

    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.

    0 讨论(0)
提交回复
热议问题