Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
Creating a simple Notepad application using Tkinter is a fun project and a great way to learn about the framework's capabilities. Here's a step-by-step tutorial to help you build a basic Notepad-like application:
Ensure you have Tkinter and Pillow (for the optional icon setting):
pip install Pillow
import tkinter as tk from tkinter import filedialog, messagebox from tkinter.scrolledtext import ScrolledText from PIL import Image, ImageTk root = tk.Tk() root.title("Tkinter Notepad") root.geometry("600x400") # Add a Text widget for user input text_area = ScrolledText(root, wrap='word') # 'word' wrap mode means that tkinter will wrap by words text_area.pack(fill=tk.BOTH, expand=1) # Make the text area expandable in both directions
# Function to open a file def open_file(): file_path = filedialog.askopenfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt"), ("All files", "*.*")]) if file_path: text_area.delete(1.0, tk.END) # Clear the text area with open(file_path, 'r') as file: text_area.insert(tk.INSERT, file.read()) # Function to save a file def save_file(): file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt"), ("All files", "*.*")]) if file_path: with open(file_path, 'w') as file: file.write(text_area.get(1.0, tk.END)) # Function to exit the application def exit_app(): if messagebox.askokcancel("Exit", "Do you really want to exit?"): root.destroy()
menu_bar = tk.Menu(root) # File menu file_menu = tk.Menu(menu_bar, tearoff=0) file_menu.add_command(label="Open", command=open_file) file_menu.add_command(label="Save", command=save_file) file_menu.add_separator() file_menu.add_command(label="Exit", command=exit_app) menu_bar.add_cascade(label="File", menu=file_menu) root.config(menu=menu_bar)
If you have an .ico
file for your application icon:
root.iconbitmap('path_to_icon.ico')
For PNG icons, with the help of Pillow:
img = ImageTk.PhotoImage(Image.open('path_to_icon.png')) root.tk.call('wm', 'iconphoto', root._w, img)
Replace 'path_to_icon.ico'
or 'path_to_icon.png'
with your icon's path.
root.mainloop()
This is a basic Notepad application using Tkinter. You can extend this further by adding features like font changing, a status bar, text highlight, undo-redo functionalities, etc.
When combined, the above code snippets will give you a functional (though minimal) Notepad application. If you're planning on making this a real project, you should consider structuring your code more modularly and implementing additional error handling and features.
Python Tkinter text editor example:
import tkinter as tk root = tk.Tk() root.title("Text Editor Example") text_widget = tk.Text(root, wrap="word") text_widget.pack(expand="yes", fill="both") root.mainloop()
Creating a simple Notepad in Tkinter:
import tkinter as tk from tkinter import filedialog def open_file(): file_path = filedialog.askopenfilename() with open(file_path, 'r') as file: content = file.read() text_widget.delete(1.0, tk.END) text_widget.insert(tk.END, content) def save_file(): file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt")]) with open(file_path, 'w') as file: content = text_widget.get(1.0, tk.END) file.write(content) root = tk.Tk() root.title("Notepad Example") menu_bar = tk.Menu(root) root.config(menu=menu_bar) file_menu = tk.Menu(menu_bar, tearoff=0) menu_bar.add_cascade(label="File", menu=file_menu) file_menu.add_command(label="Open", command=open_file) file_menu.add_command(label="Save", command=save_file) file_menu.add_separator() file_menu.add_command(label="Exit", command=root.destroy) text_widget = tk.Text(root, wrap="word") text_widget.pack(expand="yes", fill="both") root.mainloop()
Building a basic text editor with Tkinter:
import tkinter as tk from tkinter import filedialog def open_file(): file_path = filedialog.askopenfilename() with open(file_path, 'r') as file: content = file.read() text_widget.delete(1.0, tk.END) text_widget.insert(tk.END, content) def save_file(): file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt")]) with open(file_path, 'w') as file: content = text_widget.get(1.0, tk.END) file.write(content) def cut_text(): text_widget.event_generate("<<Cut>>") def copy_text(): text_widget.event_generate("<<Copy>>") def paste_text(): text_widget.event_generate("<<Paste>>") def undo_text(): text_widget.event_generate("<<Undo>>") root = tk.Tk() root.title("Basic Text Editor Example") menu_bar = tk.Menu(root) root.config(menu=menu_bar) file_menu = tk.Menu(menu_bar, tearoff=0) menu_bar.add_cascade(label="File", menu=file_menu) file_menu.add_command(label="Open", command=open_file) file_menu.add_command(label="Save", command=save_file) file_menu.add_separator() file_menu.add_command(label="Exit", command=root.destroy) edit_menu = tk.Menu(menu_bar, tearoff=0) menu_bar.add_cascade(label="Edit", menu=edit_menu) edit_menu.add_command(label="Cut", command=cut_text) edit_menu.add_command(label="Copy", command=copy_text) edit_menu.add_command(label="Paste", command=paste_text) edit_menu.add_command(label="Undo", command=undo_text) text_widget = tk.Text(root, wrap="word") text_widget.pack(expand="yes", fill="both") root.mainloop()
Tkinter text widget formatting for a Notepad app:
import tkinter as tk from tkinter import filedialog def open_file(): file_path = filedialog.askopenfilename() with open(file_path, 'r') as file: content = file.read() text_widget.delete(1.0, tk.END) text_widget.insert(tk.END, content) def save_file(): file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt")]) with open(file_path, 'w') as file: content = text_widget.get(1.0, tk.END) file.write(content) def cut_text(): text_widget.event_generate("<<Cut>>") def copy_text(): text_widget.event_generate("<<Copy>>") def paste_text(): text_widget.event_generate("<<Paste>>") def undo_text(): text_widget.event_generate("<<Undo>>") def change_font(): font_dialog = tk.FontDialog(root, text_widget) font_dialog.show() root = tk.Tk() root.title("Notepad with Text Formatting") menu_bar = tk.Menu(root) root.config(menu=menu_bar) file_menu = tk.Menu(menu_bar, tearoff=0) menu_bar.add_cascade(label="File", menu=file_menu) file_menu.add_command(label="Open", command=open_file) file_menu.add_command(label="Save", command=save_file) file_menu.add_separator() file_menu.add_command(label="Exit", command=root.destroy) edit_menu = tk.Menu(menu_bar, tearoff=0) menu_bar.add_cascade(label="Edit", menu=edit_menu) edit_menu.add_command(label="Cut", command=cut_text) edit_menu.add_command(label="Copy", command=copy_text) edit_menu.add_command(label="Paste", command=paste_text) edit_menu.add_command(label="Undo", command=undo_text) format_menu = tk.Menu(menu_bar, tearoff=0) menu_bar.add_cascade(label="Format", menu=format_menu) format_menu.add_command(label="Change Font", command=change_font) text_widget = tk.Text(root, wrap="word") text_widget.pack(expand="yes", fill="both") root.mainloop()