Skip to content

Instantly share code, notes, and snippets.

@tux-peng
Created February 13, 2026 22:53
Show Gist options
  • Select an option

  • Save tux-peng/4306abf53121817d8e45563cb948fcec to your computer and use it in GitHub Desktop.

Select an option

Save tux-peng/4306abf53121817d8e45563cb948fcec to your computer and use it in GitHub Desktop.
Tkinter, notepad. This script includes the core features: a text area, a menu bar (File/Edit/Help), and basic file operations (Open, Save, Exit).
import tkinter as tk
from tkinter import filedialog, messagebox
class Notepad:
def __init__(self, root):
self.root = root
self.root.title("Untitled - Notepad Clone")
self.root.geometry("800x600")
# Create the main text area
self.textarea = tk.Text(self.root, undo=True, font=("Consolas", 11))
self.textarea.pack(fill=tk.BOTH, expand=True)
# Create the Menu Bar
self.menubar = tk.Menu(self.root)
self.root.config(menu=self.menubar)
# File Menu
self.file_menu = tk.Menu(self.menubar, tearoff=0)
self.menubar.add_cascade(label="File", menu=self.file_menu)
self.file_menu.add_command(label="New", command=self.new_file)
self.file_menu.add_command(label="Open...", command=self.open_file)
self.file_menu.add_command(label="Save", command=self.save_file)
self.file_menu.add_separator()
self.file_menu.add_command(label="Exit", command=self.root.quit)
# Edit Menu
self.edit_menu = tk.Menu(self.menubar, tearoff=0)
self.menubar.add_cascade(label="Edit", menu=self.edit_menu)
self.edit_menu.add_command(label="Undo", command=self.textarea.edit_undo)
self.edit_menu.add_command(label="Redo", command=self.textarea.edit_redo)
self.edit_menu.add_separator()
self.edit_menu.add_command(label="Cut", command=lambda: self.root.focus_get().event_generate("<<Cut>>"))
self.edit_menu.add_command(label="Copy", command=lambda: self.root.focus_get().event_generate("<<Copy>>"))
self.edit_menu.add_command(label="Paste", command=lambda: self.root.focus_get().event_generate("<<Paste>>"))
# Help Menu
self.help_menu = tk.Menu(self.menubar, tearoff=0)
self.menubar.add_cascade(label="Help", menu=self.help_menu)
self.help_menu.add_command(label="About Notepad", command=self.show_about)
self.file_path = None
def new_file(self):
self.root.title("Untitled - Notepad Clone")
self.textarea.delete(1.0, tk.END)
self.file_path = None
def open_file(self):
self.file_path = filedialog.askopenfilename(defaultextension=".txt",
filetypes=[("Text Documents", "*.txt"), ("All Files", "*.*")])
if self.file_path:
self.root.title(f"{self.file_path} - Notepad Clone")
self.textarea.delete(1.0, tk.END)
with open(self.file_path, "r") as file:
self.textarea.insert(1.0, file.read())
def save_file(self):
if self.file_path is None:
self.file_path = filedialog.asksaveasfilename(initialfile='Untitled.txt',
defaultextension=".txt",
filetypes=[("Text Documents", "*.txt"), ("All Files", "*.*")])
if self.file_path:
with open(self.file_path, "w") as file:
file.write(self.textarea.get(1.0, tk.END))
self.root.title(f"{self.file_path} - Notepad Clone")
def show_about(self):
messagebox.showinfo("About", "This is a simple Notepad clone built with Python and Tkinter.")
if __name__ == "__main__":
root = tk.Tk()
notepad = Notepad(root)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment