Created
November 27, 2023 04:25
-
-
Save aubm/5e54dbd45540c5d0987c988f2259a2dd to your computer and use it in GitHub Desktop.
Sample Python/tkinter app with navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter as tk | |
class SampleApp: | |
def __init__(self, root): | |
self.root = root | |
self.root.title("Tkinter Navigation Example") | |
# Set the initial size of the window | |
self.root.geometry("400x300") | |
# Create a menu bar | |
self.menu_bar = tk.Menu(root) | |
root.config(menu=self.menu_bar) | |
# Create a menu "File" with options "Home" and "About" | |
self.file_menu = tk.Menu(self.menu_bar, tearoff=0) | |
self.menu_bar.add_cascade(label="File", menu=self.file_menu) | |
self.file_menu.add_command(label="Home", command=self.show_home) | |
self.file_menu.add_command(label="About", command=self.show_about) | |
self.file_menu.add_separator() | |
self.file_menu.add_command(label="Exit", command=root.destroy) | |
# Initialize the Home page | |
self.home_page() | |
def show_home(self): | |
# Destroy the current page and show the Home page | |
self.destroy_current_page() | |
self.home_page() | |
def show_about(self): | |
# Destroy the current page and show the About page | |
self.destroy_current_page() | |
self.about_page() | |
def destroy_current_page(self): | |
# Destroy all widgets in the current page | |
for widget in self.root.winfo_children(): | |
widget.destroy() | |
def home_page(self): | |
# Create widgets for the Home page | |
label = tk.Label(self.root, text="Welcome to the Home Page!", font=("Helvetica", 16)) | |
label.pack(pady=20) | |
# Add more widgets as needed for the Home page | |
def about_page(self): | |
# Create widgets for the About page | |
label = tk.Label(self.root, text="This is the About Page!", font=("Helvetica", 16)) | |
label.pack(pady=20) | |
# Add more widgets as needed for the About page | |
# Create the main Tkinter window | |
root = tk.Tk() | |
# Create an instance of the SampleApp class | |
app = SampleApp(root) | |
# Run the Tkinter event loop | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment