简体   繁体   中英

Can't understand pack and grid geometry with tkinter

Hi I didn't really understand how furas made the below code work. 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.

Can someone please explain to me how this "magic" works?

a link to the his answer: Creating new entry boxes with button 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. Each widget that acts as a container for other widgets can use either grid or pack . You just can't use both grid and pack together for widgets that have the same master.

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. 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

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. In this window we will put everything else. Then we create myFrame . Note, that the actual "thing" (in more adequate terms - widget) is created in line 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. 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. Because of that, if you want to add something more to the root in our case, you will have to use .pack() again. 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. 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. Therefore we can again use .pack() or switch to .grid() .

To conclude: .pack() , .grid() and .place() are sets of rules on how place widgets inside other widgets. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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