简体   繁体   English

无法使用tkinter理解包和网格的几何形状

[英]Can't understand pack and grid geometry with tkinter

Hi I didn't really understand how furas made the below code work. 嗨,我不太了解furas如何使以下代码起作用。 Why didn't he get an error message about grid and pack on the same root when he added a box? 当他添加一个盒子时,为什么他没有收到关于网格和打包的错误消息? In the addbox function he sets a frame to the root which is pack already and even uses the pack inside the function and then uses the grid. 在addbox函数中,他将框架设置为已经打包的根,甚至使用函数内部的pack,然后使用网格。

Can someone please explain to me how this "magic" works? 有人可以向我解释一下这种“魔术”是如何工作的吗?

a link to the his answer: Creating new entry boxes with button Tkinter 他的答案的链接: 用Tkinter按钮创建新的输入框

    from Tkinter import *

#------------------------------------

def addBox():
    print "ADD"

    frame = Frame(root)
    frame.pack()

    Label(frame, text='From').grid(row=0, column=0)

    ent1 = Entry(frame)
    ent1.grid(row=1, column=0)

    Label(frame, text='To').grid(row=0, column=1)

    ent2 = Entry(frame)
    ent2.grid(row=1, column=1)

    all_entries.append( (ent1, ent2) )

#------------------------------------

def showEntries():

    for number, (ent1, ent2) in enumerate(all_entries):
        print number, ent1.get(), ent2.get()

#------------------------------------

all_entries = []

root = Tk()

showButton = Button(root, text='Show all text', command=showEntries)
showButton.pack()

Thanks 谢谢

There's no magic, it's just working as designed. 没有魔术,只是按设计工作。 The code uses pack in the root window, and uses grid inside a frame. 该代码在根窗口中使用pack ,并在框架内使用grid Each widget that acts as a container for other widgets can use either grid or pack . 每个充当其他小部件容器的小部件都可以使用gridpack You just can't use both grid and pack together for widgets that have the same master. 对于具有相同母版的小部件,您不能同时使用gridpack在一起。

not really an answer but I think you will be helped by the link. 并非真正的答案,但我认为您会从该链接中获得帮助。 tkinter and it's layout is indeed a bit hard to understand. tkinter及其布局确实有点难以理解。 I never understood how to deal with it until I stumbled over this presentation which explained the layout particulars in a way where I finally could get the hang of it. 直到我迷失了本演示文稿,直到我终于可以理解它的方式解释了布局细节之后,我才知道如何处理它。

Just putting it out there for others to find as well. 只要把它放在那里,其他人也可以找到。 tkinter tutorial by beazley bezley的tkinter教程

I think you miss out on what pack and grid actually are. 我认为您错过了实际的包装和网格。 Consider such code: 考虑这样的代码:

import tkinter as tk

root = tk.Tk()

myFrame = tk.Frame(root)

myFrame.pack()

myButton1 = tk.Button(myFrame, text='This is button 1')
myButton2 = tk.Button(myFrame, text='This is button 2')

myButton1.grid(row=0, column=0)
myButton2.grid(row=1, column=0)

root.mainloop()

By creating root we create a new window. 通过创建root我们创建了一个新窗口。 In this window we will put everything else. 在此窗口中,我们将放置其他所有内容。 Then we create myFrame . 然后我们创建myFrame Note, that the actual "thing" (in more adequate terms - widget) is created in line myFrame = tk.Frame(root) . 注意,实际的“事物”(用更恰当的术语-小部件)是在myFrame = tk.Frame(root)行中创建的。 Note, that we have to specify where we are going to put this widget in brackets and we've written that it is going to be root - our main window. 注意,我们必须指定要将此小部件放在方括号中的位置,并且我们已经写好它将成为根目录-我们的主窗口。 Blank frame probably isn't the best example since you can not see it being placed (not unless you use some more specifications at least), but still. 空白框可能不是最好的示例,因为您看不到它的放置位置(除非您至少使用了更多规格,否则不会),但仍然如此。 We have created it, but not placed it in our user interface. 我们已经创建了它,但是没有将其放置在用户界面中。 The we use .pack() to place it. 我们使用.pack()放置它。 Now you refer to widgets as being used as packs or grids. 现在,您将小部件称为包或网格。 That is not true though. 但这不是事实。 Pack and grid are just the set of rules, on which the widgets are being placed inside some kind of window. Pack和grid只是一组规则,在其上将小部件放置在某种类型的窗口中。 Because of that, if you want to add something more to the root in our case, you will have to use .pack() again. 因此,如果要在本例中向root添加更多内容,则必须再次使用.pack() Why? 为什么? If you will give two sets of rules on how to place things on the screen for your computer - they will most likely conflict with each other. 如果您针对如何在计算机屏幕上放置东西给出两套规则-它们很可能会相互冲突。 However, if we go one more level down and now want to place something inside our myFrame , we can again choose which set of rules to use. 但是,如果我们再下一层,现在想在myFrame放置一些东西,我们可以再次选择要使用的规则集。 It is because it does not matter, where our frame is going to end up inside root , we now just want to specify where our Buttons 1 and 2 are going to end up inside the frame. 这是因为没有关系,我们的frame将在root内结束,我们现在只想指定按钮1和2在框架内的结束位置。 Therefore we can again use .pack() or switch to .grid() . 因此,我们可以再次使用.pack()或切换到.grid()

To conclude: .pack() , .grid() and .place() are sets of rules on how place widgets inside other widgets. 得出以下结论: .pack() .grid().place()是关于如何将小部件放置在其他小部件内的规则集。 In more general terms though these are rules on how place boxes in other boxes. 概括地说,这些是关于如何将框放置在其他框中的规则。 One boxes in which we arrange other boxes can only have one set of rules. 我们在其中布置其他盒子的一个盒子只能有一组规则。

I hope this example helps. 希望这个例子对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM