Multiple page application needs an option wherein users can switch or toggle among different pages. So in this tutorial, we will learn how to go to the next page in Python Tkinter Program.
When there is a reference to the next page added on a particular widget and the user clicks on that widget then he moves to the next page. One thing more happens at the same time is that the previous page is closed or destroyed.
New to Python Tkinter? Check out Python GUI Programming
Go to next page in Python Tkinter
Every .py file can be treated as a module and we can import them as and when required. On the other hand, we can destroy the previous page using the built-in method destroy. Let’s see things in action in this small program.
Code Snippet:
- In this code, We have created 3 pages and each page has two buttons.
- the first button will navigate to the next page & the other is to go to the previous page. Clicking on either of the pages will trigger a function wherein the current page will be destroyed and a new page will be imported.
- Pages are named as page1, page2, and page3
- All the pages have almost similar code. The major thing to be noticed is the import pages. Every page is importing different pages.
def nextPage():
ws.destroy()
import page2
- nextPage() : it is a function name
- ws.destroy() : This will close current page
- import page2 : page2 will start showing up.
page 1
This is the source code to create page1.py. Create a new file with the name page1.py and then copy and paste the below code.
from tkinter import *
ws = Tk()
ws.geometry('400x300')
ws.title('PythonGuides')
ws['bg']='#5d8a82'
f = ("Times bold", 14)
def nextPage():
ws.destroy()
import page2
def prevPage():
ws.destroy()
import page3
Label(
ws,
text="This is First page",
padx=20,
pady=20,
bg='#5d8a82',
font=f
).pack(expand=True, fill=BOTH)
Button(
ws,
text="Previous Page",
font=f,
command=nextPage
).pack(fill=X, expand=TRUE, side=LEFT)
Button(
ws,
text="Next Page",
font=f,
command=prevPage
).pack(fill=X, expand=TRUE, side=LEFT)
ws.mainloop()
Read: Registration form in Python using Tkinter
Page 2
This is the source code to create page2.py. Create a new file with the name page2.py and then copy and paste the below code.
from tkinter import *
ws = Tk()
ws.geometry('400x300')
ws.title('PythonGuides')
ws['bg']='#ffbf00'
f = ("Times bold", 14)
def nextPage():
ws.destroy()
import page3
def prevPage():
ws.destroy()
import page1
Label(
ws,
text="This is Second page",
padx=20,
pady=20,
bg='#ffbf00',
font=f
).pack(expand=True, fill=BOTH)
Button(
ws,
text="Previous Page",
font=f,
command=nextPage
).pack(fill=X, expand=TRUE, side=LEFT)
Button(
ws,
text="Next Page",
font=f,
command=prevPage
).pack(fill=X, expand=TRUE, side=LEFT)
ws.mainloop()
Page 3
This is the source code to create page3.py. Create a new file with the name page3.py and then copy and paste the below code.
from tkinter import *
ws = Tk()
ws.geometry('400x300')
ws.title('PythonGuides')
ws['bg']='#ffbf00'
f = ("Times bold", 14)
def nextPage():
ws.destroy()
import page1
def prevPage():
ws.destroy()
import page2
Label(
ws,
text="This is Third page",
font = f,
padx=20,
pady=20,
bg='#bfff00'
).pack(expand=True, fill=BOTH)
Button(
ws,
text="Previous Page",
font=f,
command=nextPage
).pack(fill=X, expand=TRUE, side=LEFT)
Button(
ws,
text="Next Page",
font = f,
command=prevPage
).pack(fill=X, expand=TRUE, side=LEFT)
ws.mainloop()
Output:
In this output, you can see three pages. Each page has a Label & two buttons. These buttons are to take user to the previous page or next page.
You may like the following tutorials:
- Python Tkinter Frame
- How to make a calculator in Python
- Python Tkinter Listbox
- Python Tkinter Stopwatch
- Python Tkinter Progress bar
- Create a game using Python Pygame
- Python Tkinter OptionMenu
So in this tutorial, we have learned how to go to the next page in Python Tkinter Program. In case you have any doubt or you have a better way to do so please do write in the comments below.
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.