Tkinter Modern Toggle Menu: Switching Between Multiple Pages in Tkinter GUI (Part 2)

Posted by

In this tutorial, we will learn how to create a modern toggle menu in Tkinter that will allow us to switch between multiple pages in a Tkinter GUI. This will help us create a more dynamic and user-friendly interface for our Tkinter applications.

Part 1 of this tutorial focused on creating the basic structure of our Tkinter GUI with multiple pages. In Part 2, we will add a modern toggle menu that will allow us to switch between these pages with a single click. Let’s get started!

Step 1: Create the Toggle Menu
To create the toggle menu, we will use the ttk.Notebook widget in Tkinter. This widget is used to create a tabbed interface for switching between multiple pages. We will add a set of buttons that will serve as the tabs for our pages.

First, import the required modules:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tkinter Modern Toggle Menu</title>
</head>
<body>
<!-- HTML code for the toggle menu -->
</body>
</html>

Step 2: Define the Pages
Next, we need to define the pages that we want to switch between. For this tutorial, we will create two pages: Page 1 and Page 2. Each page will contain some dummy content.

<!-- HTML code for Page 1 -->
<div id="page1" style="display: block;">
<h1>This is Page 1</h1>
<p>Welcome to Page 1</p>
</div>
<!-- HTML code for Page 2 -->
<div id="page2" style="display: none;">
<h1>This is Page 2</h1>
<p>Welcome to Page 2</p>
</div>

Step 3: Add Functionality to the Toggle Menu
Now, we will add functionality to the toggle menu buttons so that they can switch between the pages when clicked. We will use JavaScript to achieve this.

<!-- JavaScript code for the toggle menu -->
<script>
let activePage = "page1";

function showPage(pageId) {
document.getElementById(activePage).style.display = "none";
document.getElementById(pageId).style.display = "block";
activePage = pageId;
}
</script>

Step 4: Create the Toggle Menu Buttons
Finally, we will create the toggle menu buttons that will allow us to switch between the pages. We will add an onclick event to each button that will call the showPage function with the corresponding page ID.

<!-- HTML code for the toggle menu buttons -->
<button onclick="showPage('page1')">Page 1</button>
<button onclick="showPage('page2')">Page 2</button>

That’s it! You have successfully created a modern toggle menu in Tkinter that allows you to switch between multiple pages in your Tkinter GUI. You can customize the design and content of the pages as needed to fit your application requirements.

I hope you found this tutorial helpful. Thank you for reading!

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@user-tk9qb3yz5o
2 months ago

Friend views video par kaisy aien thora bstaen plz😢

@vinhbaonguyendinh9986
2 months ago

that is .great 🎉