1

I am trying to create a program in which the user navigates through the screens with "Next" and "Previous" buttons. Editing the code from the selected answer here I have produced the following code:

import Tkinter as tk
import tkFont as tkfont        

def cancel():
    root.destroy()

def disable_event():
    pass

#Set the parent (main/root) window
root = tk.Tk()
root.title("Some title")
root.geometry('700x500') #Width x Height
root.resizable(0, 0) #Make root unresizable and force the use of "Cancel" button
root.protocol("WM_DELETE_WINDOW", disable_event)


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

        self.title_font = tkfont.Font(family='Helvetica', size=18)
        container = tk.Frame(root)
        container.pack(side="top", fill="both", expand=True)
        self.frames = {}
        for F in (Intro, FirstPage):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
        self.show_frame("Intro")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

class Intro(tk.Frame):
    def __init__(self, parent, controller):
        Intro = tk.Frame.__init__(self, parent)
        self.controller = controller
        leftFrame = tk.Frame(Intro, height=500, width=250, bg="#000000")
        leftFrame.pack(side="left")
        middleFrame = tk.Frame(Intro, height=500, width=5)
        middleFrame.pack(side="left")
        rightFrame = tk.Frame(Intro, height=500, width=450, bg="#FFFFFF")

        buttonFrame = tk.Frame(Intro, height=35, width=450, bg="#FFFFFF")
        nextButton = tk.Button(buttonFrame, width=10, text="Next >", command=lambda: controller.show_frame("FirstPage")).grid(row=0, column=0)
        div3 = tk.Frame(buttonFrame, bg="#FFFFFF", width=10).grid(row=0, column=1)
        cancelButton = tk.Button(buttonFrame, text="Cancel", width=10, command=cancel).grid(row=0,column=2)
        buttonFrame.pack_propagate(False)
        buttonFrame.pack(side="bottom")
        #Other child widgets to rightFrame managed by pack

        rightFrame.pack_propagate(False)
        rightFrame.pack_forget()
        rightFrame.pack(side="right")

class FirstPage(tk.Frame):
        #the same code with "previousButton"

However, when I execute the code I notice that even if the show_frame function is called the FirstPage does not appear and that if I resize the main window it gradually appears from behind the Intro. When I run the original code, it works perfectly.

Is the problem because I am using the pack() manager while the original code uses grid() or what? Can somebody provide with a sample code?

P.S.: I have seen other questions but they all use grid(). I am using python 2.7.

1 Answers1

1

You simply cannot use pack to overlay one widget on top of another within the same master. That's just not something that pack can do. pack is explicitly designed to place widgets in unallocated space above, below, or to the side of existing widgets in the same master.

If you want to stack frames on top of each other, you need to use either grid or place.

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