0

So, I'm attempting to make a GUI on python using tkinter for the first time, but I'm having trouble with the Pack() attribute.

My code for the GUI thus far is as follows:

mainframe = tk.Tk()
mainframe.title("Hostname2IP")
mainframe.columnconfigure(1, weight=1)
mainframe.rowconfigure(1, weight=1)

SC= tk.scrolledtext.ScrolledText #shorten code a bit...
SC(master=mainframe, wrap=tk.WORD, width=5, height=15).grid(column=1, row=1, sticky=("NSEW"))
SC(master=mainframe, wrap=tk.WORD, width=5, height=15).grid(column=2, row=1, sticky=("NSEW"))
SC(master=mainframe, wrap=tk.WORD, width=5, height=15).grid(column=3, row=1, sticky=("NSEW"))

L1 = tk.Label(master=mainframe, text="Label1").grid(column=1, row=1, sticky="S")
L1 = tk.Label(master=mainframe, text="Label2").grid(column=2, row=1, sticky="S")
L1 = tk.Label(master=mainframe, text="Label3").grid(column=3, row=1, sticky="S")
tk.Button(master=mainframe, text="Open Targetslist", command=OpenTargetsButton).grid(column=1,row=3,sticky="W")
tk.Button(master=mainframe, text="Save Output", command=WriteOutButton).grid(column=2,row=3,sticky="E")
tk.Button(master=mainframe, text="Go", command=lambda: sequence(FilterList(file_path),\
                                                          GetIP_Mac(targethostlist),\
                                                          WriteOutput(Out_path))).\
                                                          grid(column=2, row=4, sticky="SE")

#bind key to a command
mainframe.bind('<Return>', lambda: sequence(FilterList(file_path),GetIP_Mac(targethostlist),WriteOutput(Out_path)))
mainframe.mainloop()

Running this displays:

enter image description here

As you can see, the labels are overlapping... And everything is uneven and messy too.

My issue right now is that I cannot apply Pack() to L1,L2,L3 or any alike.

Doing: L1.Pack(side="bottom") tells me " Theres no attribute 'pack' ". When I tried removing the grid(...) config there, gives me another error:

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

So.. what am I doing wrong here?

EDIT: As said in the answers below, and in the last error, I had to use one geometry manager. I used grid alone on the (only) window container and so it worked out fine. Pretty simple now actually.

DonD
  • 339
  • 3
  • 17
  • 1
    L1 = tk.Label(master=mainframe, text="Label1").grid(column=1, row=1, sticky="S") should be splitted in two lines... – Eric Levieil Apr 12 '15 at 20:37

2 Answers2

1

You are using grid and pack in the same window. Tkinter can only deal with one geometry manager in a window, so decide if you want grid or pack.

VOT Productions
  • 136
  • 1
  • 9
  • "in one window" is a bit ambiguous. It's more correct to say you can't use both within the same parent or container. You can certainly use them both inside a window as a whole, if you use them in different frames within the window. – Bryan Oakley Apr 13 '15 at 20:28
  • Yeah, it wasn't the greatest answer in the world. – VOT Productions Apr 14 '15 at 08:52
1

Two things. First, pack is lowercase. I don't know if that was a typo or if you had it in your code.

Now the main thing: Only one type of geometry manager per container.

If you pack something into a container, everything else that directly goes into that container must also be packed.

This is not to say that only one kind of geometry management can be used in your whole tkinter program, just per container.

Containers could be: TopLevel (a window), a Frame, a LabelFrame, a PanedWindow, or a NoteBook. In other words, any widget that you put other widgets into.

So if you start with your main Tk instance, a TopLevel, and you put widgets into it, whichever method you use ( grid/pack/place ) you have to use for all widgets DIRECTLY CONTAINED by that container.

However, if one of those widgets is itself a container, say a Frame, you can use a different method of geometry management to arrange widgets INSIDE that widget.

dusty
  • 865
  • 9
  • 15