-1

I am trying to use the grid function instead of the pack function in Tkinter so I can arrange my widgets better, however it is not working. I get this error message:

cannot use geometry manager pack inside .!frame.!startpage which already has slaves managed by grid

Any help would be appreciated. Scroll down to see where I have used grid, I have left a comment.

import tkinter as tk
from tkinter import ttk
from tkcalendar import *

LARGE_FONT= ("Verdana", 12)

class StudyFriendO(tk.Tk):

    def __init__(self, *args, **kwargs):

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

        self.title("StudyFriendO") #naming window title
        self.geometry('850x830') #size of window

        
        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, HomePage, ToDoPage, TimetablePage): #list of multiple frames of program

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class StartPage(tk.Frame): #creating start page

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="StudyFriendO", font = LARGE_FONT)
        label.grid(row=1, column=2) #HERE IS WHERE I USE GRID

        
        cal = Calendar(self, background="#e0f6fc", disabledbackground="white", bordercolor="light blue", headersbackground="light blue", normalbackground="#e0f6fc", foreground="black", normalforeground='black', headersforeground='white', selectmode="day", year=2021, month=8, day=9)
        cal.pack(pady=20 ) #calendar


        button1 = ttk.Button(self, text="Enter",
                            command=lambda: controller.show_frame(HomePage)) #button to navigate page
        button1.pack()
        

class HomePage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Home", font = LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = ttk.Button(self, text="To Do",
                            command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
        button1.pack()

        button1 = ttk.Button(self, text="Timetable",
                            command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
        button1.pack()

        button1 = ttk.Button(self, text="Home",
                            command=lambda: controller.show_frame(HomePage)) #button to navigate page
        button1.pack()


class ToDoPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="To Do", font = LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = ttk.Button(self, text="Home",
                            command=lambda: controller.show_frame(HomePage)) #button to navigate page
        button1.pack()

        button1 = ttk.Button(self, text="Timetable",
                            command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
        button1.pack()

        button1 = ttk.Button(self, text="To Do",
                            command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
        button1.pack()


class TimetablePage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Timetable", font = LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = ttk.Button(self, text="Home",
                            command=lambda: controller.show_frame(HomePage)) #button to navigate page
        button1.pack()

        button1 = ttk.Button(self, text="Timetable",
                            command=lambda: controller.show_frame(TimetablePage)) #button to navigate page
        button1.pack()

        button1 = ttk.Button(self, text="To Do",
                            command=lambda: controller.show_frame(ToDoPage)) #button to navigate page
        button1.pack()

app = StudyFriendO()
app.mainloop()
D_00
  • 1,440
  • 2
  • 13
  • 32
Minbutt
  • 59
  • 1
  • 6
  • 4
    Doesn't the error message make this clear? You have to choose `.pack()` or `.grid()` for *all* of the children of any container, you can't mix them. The error isn't occurring at the point where you use `.grid()` on your Label, it happens when you use `.pack()` on the siblings of the Label. – jasonharper Sep 28 '21 at 11:52

1 Answers1

0

It already has slaves manages by grid, either use pack or grid but don't mix them up

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 29 '21 at 06:06