-1

I'm using below example, found on this portal. And try to add listbox. I know that I can use pack, instead grid, but pack is quite messy, and with grid I can be more specific.

 import tkinter as tk
 TITLE_FONT = ("Helvetica", 18, "bold")
 class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, c):
        '''Show a frame for the given class'''
        frame = self.frames[c]
        frame.tkraise()


 class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="This is the start page",  font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Page One",
                            command=lambda: controller.show_frame(PageOne))
        button2 = tk.Button(self, text="Go to Page Two",
                            command=lambda: controller.show_frame(PageTwo))
        button1.pack()
        button2.pack()
 class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame(StartPage))
        button.pack()
 class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="This is page 2", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame(StartPage))
        button.pack()
 if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

And for example I want add this to PageTwo class. But I don't want to use .pack.

    lbox = tk.Listbox(self, height=10)
    lbox.grid(column=0, row=0, padx=5)
    s = ttk.Scrollbar(self, orient="vertical", command=lbox.yview)
    s.gird(column=1, row=1, sticky=("s","e"))

And I getting this error

C:\Python34\python.exe C:/Users/Mariusz/OneDrive/Projekty/Python/himan/main.py
Traceback (most recent call last):
  File "C:/Users/Mariusz/OneDrive/Projekty/Python/himan/main.py", line 198, in <module>
    app = Himan()
  File "C:/Users/Mariusz/OneDrive/Projekty/Python/himan/main.py", line 44, in __init__
    frame = F(container, self)
  File "C:/Users/Mariusz/OneDrive/Projekty/Python/himan/main.py", line 132, in __init__
    lbox.grid(column=0, row=0, padx=5)
  File "C:\Python34\lib\tkinter\__init__.py", line 2057, in grid_configure
    + self._options(cnf, kw))
_tkinter.TclError: cannot use geometry manager grid inside .52613520.52708464 which already has slaves managed by pack

Process finished with exit code 1
patthoyts
  • 32,320
  • 3
  • 62
  • 93

2 Answers2

0

You cannot mix pack and grid together within the same container. It is possible to mix them in different containers, but I'd highly recommend staying with only one of these geometry manager.

See this question for some more help.

Community
  • 1
  • 1
I8086
  • 291
  • 1
  • 8
0

As long as you use only pack or only grid inside of PageTwo you're fine. As the error message is telling you, you're attempting to use both at the same time. If you want to use grid inside that class, make sure you use only grid for children of PageTwo.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685