NotePad Using Python

Introduction:

tkinter is a GUI library provided by Python to create GUI applications. In this project, with the help of this library, we are going to build up the notepad, a text editor. The notepad will have two main menu items: File & edit. The functionalities of these menu items will also be there.

Explanation:

First of all, we will import the tkinter library. With this, we will also import the “filedailog” module from tkinter which will provide the classes and factory functions for creating file selection windows.

Now, we will initiate a class named “Notepad ” by using the “class” keyword. During this process, we will pass the parameter as “tk. Tk” which will create the root object for our GUI application. Under this, we will define a “__init__” constructor with self,*args,**kwargs as parameters.

Under this constructor, we will define the title by using “.title” and create a Text widget with the help of the “.Text()” method and will make this widget fill the entire frame with the “.pack()” method.

For the notepad to show the menu bar we will create the menu widget with the help of “.Menu()”.

The functionalities provided by the File menu in this project will be: New, Open, save and exit.

and the functionalities provided by the edit menu will be: cut, copy, and paste

The following options will be created by using the “.add_cascade()” method. This method will create a new hierarchical menu by associating the given menu with the parent menu. While doing so, we will pass the user-defined functions which will carry the particular functionality, under the “command” parameter. By this, both main menu items will get created.


Source Code




Read More Page

Code Box with Copy Button

Example Code

            
# Find More Python Projects at Codewithcurious.com
import tkinter as tk
from tkinter import filedialog

class Notepad(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # Set the title for the notepad
        self.title("Notepad")

        # Create a text widget
        self.text = tk.Text(self, wrap="word")
        self.text.pack(side="top", fill="both", expand=True)

        # Create a menu bar
        self.menu = tk.Menu(self)
        self.config(menu=self.menu)

        # Create a file menu
        file_menu = tk.Menu(self.menu)
        self.menu.add_cascade(label="File", menu=file_menu)
        file_menu.add_command(label="New", command=self.new_file)
        file_menu.add_command(label="Open", command=self.open_file)
        file_menu.add_command(label="Save", command=self.save_file)
        file_menu.add_separator()
        file_menu.add_command(label="Exit", command=self.quit)

        # Create an edit menu
        edit_menu = tk.Menu(self.menu)
        self.menu.add_cascade(label="Edit", menu=edit_menu)
        edit_menu.add_command(label="Cut", command=self.cut)
        edit_menu.add_command(label="Copy", command=self.copy)
        edit_menu.add_command(label="Paste", command=self.paste)

    def new_file(self):
        self.text.delete("1.0", "end")
        self.title("Notepad")

    def open_file(self):
        file = filedialog.askopenfile(parent=self, mode="rb", title="Open a file")
        if file:
            contents = file.read()
            self.text.delete("1.0", "end")
            self.text.insert("1.0", contents)
            file.close()
            self.title(file.name + " - Notepad")

    def save_file(self):
        file = filedialog.asksaveasfile(mode="w", defaultextension=".txt", filetypes=[("Text Documents", "*.txt"), ("All Files", "*.*")])
        if file:
            contents = self.text.get("1.0", "end")
            file.write(contents)
            file.close()
            self.title(file.name + " - Notepad")

    def cut(self):
        self.text.event_generate("<>")

    def copy(self):
        self.text.event_generate("<>")

    def paste(self):
        self.text.event_generate("<>")

if __name__ == "__main__":
    notepad = Notepad()
    notepad.mainloop()
            
        

Read More

Post a Comment

Post a Comment (0)