0

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

LittleWing
  • 21
  • 6
  • All children of `root` (namely, `showButton` and `frame`) use `.pack()`. All children of `frame` (namely, `ent1` and `ent2`) use `.grid()`. Thus, there are no conflicts. – jasonharper Jan 22 '19 at 20:35
  • Hi Jason, it doesn't matter if the frame is being used as pack?. inside of it I can still put grid widgets although the frame is displayed as pack? – LittleWing Jan 23 '19 at 07:02

3 Answers3

1

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Hi Bryan.. Could you please elaborate the add box function? The way I see it he uses ent1 and ent2 as grid, but the frame that he's referring to is pack.. That is the part that is hard for me to understand. – LittleWing Jan 22 '19 at 20:50
  • @LittleWing: I don't know how to explain it any better. All widgets that are children of the root window are using `pack`, all widgets that are children of `frame` are using `grid`. That' perfectly acceptable. From the perspective of the widgets inside of `frame` it doesn't matter how `frame` is added to its parents. – Bryan Oakley Jan 22 '19 at 20:53
  • Thank you for your explanation – LittleWing Jan 22 '19 at 20:56
0

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

ahed87
  • 1,240
  • 10
  • 10
0

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.