Changing Pages in Tkinter | Changing Frames in Tkinter | Shifting Between Multiple Pages in Tkinter

Posted by


Switching pages or frames in Tkinter is a common requirement when building GUI applications. In this tutorial, we will cover how to switch pages and frames in Tkinter to develop multi-page applications.

Step 1: Setting up the Tkinter window

To start, we need to import the Tkinter library and create a root window for our application.

import tkinter as tk

root = tk.Tk()
root.title("Switch Pages in Tkinter")
root.geometry("800x600")

Step 2: Creating multiple frames

Next, we will create multiple frames to represent different pages of our application. Each frame will contain different widgets such as buttons, labels, or entry fields.

frame1 = tk.Frame(root, bg="red", width=800, height=600)
frame2 = tk.Frame(root, bg="blue", width=800, height=600)

frame1.pack(fill='both', expand=True)

Step 3: Defining functions to switch frames

Now, we need to create functions that will switch between the frames when called. We will use the pack_forget() method to hide the current frame and the pack() method to display the new frame.

def show_frame1():
    frame1.pack()
    frame2.pack_forget()

def show_frame2():
    frame2.pack()
    frame1.pack_forget()

Step 4: Adding buttons to switch frames

We will now create buttons that will call the functions defined above to switch between the frames.

button1 = tk.Button(frame1, text="Go to Page 2", command=show_frame2)
button1.pack()

button2 = tk.Button(frame2, text="Go to Page 1", command=show_frame1)
button2.pack()

Step 5: Running the application

Finally, we can run our application by starting the Tkinter main loop.

show_frame1()
root.mainloop()

Now, you should see a window with two frames and buttons to switch between them. This is a basic example of how to switch frames in Tkinter. You can extend this concept to create more complex multi-page applications by adding more frames and buttons and defining additional functions to switch between them.

In conclusion, switching pages or frames in Tkinter is a key aspect of building GUI applications. By following the steps outlined in this tutorial, you can create multi-page applications with ease. Experiment with different widgets, layouts, and functionalities to enhance your Tkinter applications.

0 0 votes
Article Rating
22 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@namanmittal623
1 month ago

thanks bro this helped me too much this was too easy

@SofiaTimm
1 month ago

could you use a voice changer instaed? having an actual voice talking would be a HUGE difference

@Giovoish
1 month ago

Thank you <3

@Xero_Wolf
1 month ago

Thanks for the tutorial but the Bandcamp and kinemaster watermarks were just bugging me lol. You can use OBS to record your screen. It's free with no watermarks on your recordings. Also Davinci Resolve is a great video editor what has a fully functional free version.

@yuniekanand301
1 month ago

Your videos are awesome it helps me very much but it will be more helpful if you could provide the source code 🙂

@SureshKumar.23
1 month ago

Super

@-Rania-
1 month ago

what is fonction "page ()" for ?

@sg8nj
1 month ago

Excellent💯👍👏

@juniorludima2727
1 month ago

junior from nairobi,
exactly what i have been looking for, very helpful video, thanks a lot .

@furjackgaming
1 month ago

Sir plz help when I create a entry in home and enter something in it and if press about or any other option frame and go back to home the entry is empty what to do sir plz help

@syedmumer2348
1 month ago

Can we get the source code?

@prumchhangsreng979
1 month ago

best thing ever

@chityinoo6150
1 month ago

where can i get source code??

@DRogeryt
1 month ago

Could u pls tell me how to merge this code with my main application code pls reply asap

@tehseenbaig3198
1 month ago

i have a question, my interface is working but pages frame are not shown. i have followed all code but I cant show any frame when the button is clicked, need help

@HiwaHossien
1 month ago

give me source code

@bucksera475
1 month ago

it's not often I get exactly what i look for, but this one is an exception. absolutly fantastic video. thank you

@quinnjaguttis
1 month ago

in which python version is this
because in the line with main_frame.configure(width=500, heigth=400) is an error

@jonathanvioleta3578
1 month ago

source code 🙂

@souravjana6587
1 month ago

Really interesting ! Where can we find the coding in a whole ????