KEMBAR78
Python Notepad GUI with Tkinter | PDF | Text File | Menu (Computing)
0% found this document useful (0 votes)
168 views10 pages

Python Notepad GUI with Tkinter

This document describes a Python GUI notepad program. It features a scrollable text area, menu bar with File, Edit and Format options, and allows creating, opening, saving and editing text files. The source code uses Tkinter to create the GUI elements and menu structure. Methods handle file operations, text editing functions and font customization. Screenshots demonstrate the output and the conclusion states it is a powerful yet simple text editor for users.

Uploaded by

bibek64m5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views10 pages

Python Notepad GUI with Tkinter

This document describes a Python GUI notepad program. It features a scrollable text area, menu bar with File, Edit and Format options, and allows creating, opening, saving and editing text files. The source code uses Tkinter to create the GUI elements and menu structure. Methods handle file operations, text editing functions and font customization. Screenshots demonstrate the output and the conclusion states it is a powerful yet simple text editor for users.

Uploaded by

bibek64m5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

NOTEPAD

GUI
USING
PYTHON
INDEX

Sl.No Topic Page.No

1 Project Description 3

2 Source code 4-6

3 Code Explanation 7-8

4 Output Screenshots 9

5 Conclusion 10
PROJECT DESCRIPTION

This notepad GUI project is a simple text


editor designed to create, edit, and save
text files in Python. It features a user-
friendly interface, various font and font-
size options, and the ability to cut, copy,
and paste text. The program also allows the
user to open and save files in various
formats, including .txt, and .docx. It's a
perfect tool for anyone who needs a simple
text editor for their daily use.
SOURCE CODE
import tkinter as tk

import tkinter.scrolledtext as scrolledtext

import tkinter.font as font

import tkinter.simpledialog as simpledialog

class Notepad:

def __init__(self, master):

self.master = master

master.title("Your Pad")

# create scrolledtext widget

self.text_area = scrolledtext.ScrolledText(master, wrap=tk.WORD, font=("Consolas", 12))

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

# create menu bar

self.menu_bar = tk.Menu(master)

master.config(menu=self.menu_bar)

# create file menu

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="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.exit_program)

# create edit menu


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

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

self.edit_menu.add_command(label="Cut", command=self.cut)

self.edit_menu.add_command(label="Copy", command=self.copy)

self.edit_menu.add_command(label="Paste", command=self.paste)

# create format menu

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

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

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

self.format_menu.add_command(label="Font Size", command=self.set_font_size)

#Create a new Notepad

def new_file(self):

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

#Open a file

def open_file(self):

file = tk.filedialog.askopenfile(defaultextension=".txt", filetypes=[("Text Files",


"*.txt"), ("All Files", "*.*")])

if file:

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

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

file.close()

#Save the Notepad with the required extension

def save_file(self):

file = tk.filedialog.asksaveasfile(defaultextension=".txt", filetypes=[("Text Files",


"*.txt"), ("All Files", "*.*")])

if file:

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

file.close()
#Exiting the program

def exit_program(self):

self.master.destroy()

#Cut a selected text

def cut(self):

self.text_area.event_generate("<<Cut>>")

#Copy a selected text

def copy(self):

self.text_area.event_generate("<<Copy>>")

#Paste a selected text

def paste(self):

self.text_area.event_generate("<<Paste>>")

#Set the font style

def set_font(self):

font_family = simpledialog.askstring("Font Family", "Enter font family:")

if font_family:

self.text_area.configure(font=font.Font(family=font_family))

#Set the font size

def set_font_size(self):

font_size = simpledialog.askinteger("Font Size", "Enter font size:")

if font_size:

self.text_area.configure(font=font.Font(size=font_size))

root = tk.Tk()

notepad = Notepad(root)

root.mainloop()
CODE EXPLAINED
This is the source code for a simple Notepad GUI using the Tkinter library. Here is a
brief explanation of the code:

The first line imports the Tkinter library as 'tk', while the second and third lines
import the ScrolledText and Font modules respectively.

The class PyPad is defined to represent the Notepad GUI.

The constructor for the PyPad class takes in a master object (which represents the main
window of the GUI) and sets its title to "Your Pypad".

A ScrolledText widget is created using the ScrolledText module, with the wrap style set to
WORD and the font set to Consolas 12. The widget is then packed onto the master window
using the pack() method.

A menu bar is created using the Menu class and assigned to the master window using the
config() method.

The File menu is created as a cascading menu under the menu bar using the add_cascade()
method. Four menu options - New, Open, Save, and Exit - are added to the File menu using
the add_command() method.

The Edit menu is created as another cascading menu under the menu bar using the
add_cascade() method. Three menu options - Cut, Copy, and Paste - are added to the Edit
menu using the add_command() method.

The Format menu is created as a cascading menu under the menu bar using the add_cascade()
method. One menu option - Font - is added to the Format menu using the add_command()
method.

The rest of the code defines various methods that handle the actions corresponding to the
menu options added in steps 6-8.

The new_file() method deletes the contents of the ScrolledText widget when the New menu
option is selected.

The open_file() method opens a file dialog box and allows the user to select a text file
to open. If a file is selected, its contents are read and displayed in the ScrolledText
widget.

The save_file() method opens a file dialog box and allows the user to select a location to
save the contents of the ScrolledText widget. If a location is selected, the contents are
written to a text file with the .txt extension.

The exit_program() method destroys the main window of the GUI when the Exit menu option is
selected.
The cut(), copy(), and paste() methods simulate the Cut, Copy, and Paste operations
respectively on the selected text in the ScrolledText widget.

The set_font() method opens two dialog boxes - one for the user to enter the font family
and another for the user to enter the font size. If both font family and font size are
entered, a new Font object is created with the specified attributes and applied to the
ScrolledText widget using the configure() method.

The last few lines create a Tkinter root object and pass it as an argument to the PyPad
constructor to create an instance of the Notepad GUI. The mainloop() method of the root
object is then called to start the event loop and keep the GUI window open.
OUTPUT SCREENSHOTS
CONCLUSION

In conclusion, the Notepad GUI made in Python is a


powerful tool that allows users to create, edit, and
save text files easily. Its user-friendly interface,
combined with its simple yet powerful features, makes it
a great tool for both novice and advanced users alike.
With its ability to handle multiple file formats,
customizable font styles and sizes, and support for
various file operations, this application is a must-have
for anyone looking for a reliable and efficient text
editor. Overall, this project has been a success and has
provided a valuable learning experience in Python GUI
programming.

You might also like