Python se WordPad Clone Banane ka Best Tarika - Step by Step Guide

Demo :


Click Video πŸ‘‡πŸ‘‡πŸ‘‡




Features

✅ Create & Edit Text Files 

✅ Save Files in .txt Format 

✅ Open Existing Text Files 

✅ Cut, Copy, Paste Functions 

✅ Modern GUI using Tkinter


Code :


Folder Structure :


python_wordpad_clone/
│── main.py  # Main Python Script
│── README.md  # Project Description
└── assets/
    └── icon.ico  # Application Icon


import tkinter as tk

from tkinter import filedialog, messagebox, font, colorchooser


class WordPad:

    def __init__(self, root):

        self.root = root

        self.root.title("Fuzzu WordPad")

        self.root.geometry("800x600")

        

        self.text_area = tk.Text(self.root, font=("Arial", 12), undo=True)

        self.text_area.pack(expand=True, fill=tk.BOTH)

        

        self.menu_bar = tk.Menu(self.root)

        self.root.config(menu=self.menu_bar)

        

        self.file_menu = tk.Menu(self.menu_bar, tearoff=0)

        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)

        self.menu_bar.add_cascade(label="File", menu=self.file_menu)

        

        self.edit_menu = tk.Menu(self.menu_bar, tearoff=0)

        self.edit_menu.add_command(label="Undo", command=self.text_area.edit_undo)

        self.edit_menu.add_command(label="Redo", command=self.text_area.edit_redo)

        self.edit_menu.add_command(label="Find & Replace", command=self.find_replace)

        self.menu_bar.add_cascade(label="Edit", menu=self.edit_menu)

        

        self.format_menu = tk.Menu(self.menu_bar, tearoff=0)

        self.format_menu.add_command(label="Bold", command=self.make_bold)

        self.format_menu.add_command(label="Italic", command=self.make_italic)

        self.format_menu.add_command(label="Underline", command=self.make_underline)

        self.format_menu.add_command(label="Font Color", command=self.change_color)

        self.menu_bar.add_cascade(label="Format", menu=self.format_menu)

        

    def open_file(self):

        file_path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])

        if file_path:

            with open(file_path, "r") as file:

                self.text_area.delete(1.0, tk.END)

                self.text_area.insert(tk.END, file.read())

    

    def save_file(self):

        file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])

        if file_path:

            with open(file_path, "w") as file:

                file.write(self.text_area.get(1.0, tk.END))

    

    def make_bold(self):

        bold_font = font.Font(self.text_area, self.text_area.cget("font"))

        bold_font.configure(weight="bold")

        self.text_area.tag_configure("bold", font=bold_font)

        self.text_area.tag_add("bold", "sel.first", "sel.last")

    

    def make_italic(self):

        italic_font = font.Font(self.text_area, self.text_area.cget("font"))

        italic_font.configure(slant="italic")

        self.text_area.tag_configure("italic", font=italic_font)

        self.text_area.tag_add("italic", "sel.first", "sel.last")

    

    def make_underline(self):

        underline_font = font.Font(self.text_area, self.text_area.cget("font"))

        underline_font.configure(underline=True)

        self.text_area.tag_configure("underline", font=underline_font)

        self.text_area.tag_add("underline", "sel.first", "sel.last")

    

    def change_color(self):

        color = colorchooser.askcolor()[1]

        if color:

            self.text_area.tag_configure("color", foreground=color)

            self.text_area.tag_add("color", "sel.first", "sel.last")

    

    def find_replace(self):

        find_window = tk.Toplevel(self.root)

        find_window.title("Find & Replace")

        

        tk.Label(find_window, text="Find:").grid(row=0, column=0)

        find_entry = tk.Entry(find_window)

        find_entry.grid(row=0, column=1)

        

        tk.Label(find_window, text="Replace:").grid(row=1, column=0)

        replace_entry = tk.Entry(find_window)

        replace_entry.grid(row=1, column=1)

        

        def replace_text():

            find_text = find_entry.get()

            replace_text = replace_entry.get()

            content = self.text_area.get(1.0, tk.END)

            new_content = content.replace(find_text, replace_text)

            self.text_area.delete(1.0, tk.END)

            self.text_area.insert(tk.END, new_content)

        

        tk.Button(find_window, text="Replace All", command=replace_text).grid(row=2, column=0, columnspan=2)


if __name__ == "__main__":

    root = tk.Tk()

    WordPad(root)

    root.mainloop()

Comments

Popular posts from this blog

πŸš€ Simple Login & Registration System in Python Tkinter πŸ“±

πŸš€ Create a Python Screen Recorder with Audio (Complete Code)

Python IP Tracker App with GUI | Track IP Location Real-Time! (Working Project)