Pin Your Notes in Python – Sticky Notes Project
FREE Online Courses: Knowledge Awaits – Click for Free Access!
Sometimes there are some things that we want to note down and keep them in our system so that we can view them and they can remind us of a specific task we wrote. These notes can be an alarm, a recipe or make a list of things you want for your birthday. Noting these things make it so easier as we do not even have to remember it. Today we are going to create a note area where you can write such things. So let’s start with Python Pin your note project.
About Pin Your Notes Project using Python
We are going to create a Note Writing Area using Python. This Note will have a function to write, edit, update and delete the note. Using this application, you can create a number of notes and save them. You can also redo a note and see through it whenever you want. This application helps users to do more than one note at a single time too.
Let us have a look at the prerequisites for this project.
Project Prerequisites
This is an intermediate level project. We will install the Tkinter Module to create an easy GUI in python and Sqlite3 Module for creating the database. Use the following commands to install these.
- Pip install tk
- You do not need to install Sqlite3 as it is already a part of the python packages.
Download Python Pin Your Notes Project
Before proceeding with the project, download the source code from the following link: Pin Your Notes Project
Steps to Proceed with Sticky Notes Project using Python
Following are the steps to proceed with the Sticky Notes project.
- Import the required libraries
- Creating the GUI Window
- Connection with the Database
- Create Notes Functionality
- Edit Notes Functionality
- View Notes Functionality
- Delete Notes Functionality
1. Import the Required Libraries:
#importing libraries and modules import sqlite3 as sql from tkinter import * from tkinter import messagebox
- Tkinter Module – The Tkinter Module will help us create a GUI Window. Messagebox helps us display a pop up box.
- Sqlite3 Module – This module will help us create a database and after making a connection we can finally save our notes in the database.
2. Creating the GUI Window:
window=Tk() window.geometry("700x600") window.title("ProjectGurukul Pin Your Notes") Label(window,text="Pin Your Notes with ProjectGurukul").pack()
- Tk() – The Tk() method creates the window.
- title() – This is to specify the title of the window.
- geometry() – This function specifies the size and position of the window.
- Label() – Label() method helps us create a widget which will display a text on our window. In this widget, we can change the background colour, foreground colour, text font, size etc of the widget.
Note_Title_Label=Label(window,text="Note Title:").pack() Note_Title_Entry=Entry(window) Note_Title_Entry.pack() Txt_Label=Label(window,text="Enter Your Text Here:").pack() Txt=Text(window) Txt.pack() Button(window, text="Create new Notes",command=create_notes,bg='pink').pack() Button(window, text="View Notes",command=view_notes,bg='yellow').pack() Button(window, text="Edit Notes",command=edit_notes,bg='yellow').pack() Button(window, text="Delete Notes",command=delete_notes,bg='yellow').pack()
- Entry() – Entry() function helps us create an entry field on the window where the user can enter the title of the note.
- Text() – Text() method creates a text area where the user can write down their note content.
- Button() – The Button() function creates a button on the main window. It has a number of attributes like colour of button,text on the button, font, size of the button etc. Here we have created 4 buttons :
1. Create Notes Button
2. Edit Notes Button
3. View Notes Button
4. Delete Notes Button
- pack() – To display all the widgets that we have created, we use the pack() method. The pack() method doesn’t require any coordinate specification. It displays the widget automatically.
3. Connecting to the DataBase:
# Create database connection and connect to table try: con = sql.connect('ProjectGurukul.db') cur = con.cursor() cur.execute('''CREATE TABLE notes_table ( notes_title text, notes text)''') except: print("Connected to table of database")
- sql.connect() – This method creates a database (here the database name is ProjectGurukul) and then connects to the database. If there is an already existing database then a connection is established.
- con.cursor() – This creates a cursor which will help us with the execution of the queries.
- execute() – This executes the queries with the help of the cursor.
4. Create Notes Functionality:
def create_notes(): #Get input values notes_title = Note_Title_Entry.get() notes = Txt.get("1.0", "end-1c") #Raise a prompt for missing values if (len(notes_title)<=0) & (len(notes)<=1): messagebox.showerror(message = "Enter Details" ) else: #Insert into the table cur.execute("INSERT INTO notes_table VALUES ('%s','%s')" %( notes_title, notes)) messagebox.showinfo(message="Note added") #Commit to preserve the changes con.commit()
- This function will be evoked everytime, “Create a Note” button is clicked. This will help us create a new note.
- get() – Using the get() method the title name and text entered.
- Then using the if else loop we display different Message Boxes. If the title or text area is empty, we will display the message that details need to be entered.
- When we enter the details, we execute the query and add the details of the note in the database using the execute() method. Using the commit() method, we save the changes.
5. Edit Button Functionality:
#Update the notes def edit_notes(): #Obtain user input notes_title = Note_Title_Entry.get() notes = Txt.get("1.0", "end-1c") #Check if input is given by the user if (len(notes_title)<=0) & (len(notes)<=1): messagebox.showerror(message = "Enter Details:" ) #update the note else: sql_statement = "UPDATE notes_table SET notes = '%s' and notes_title ='%s'" %(notes, notes_title) cur.execute(sql_statement) messagebox.showinfo(message="Note Edited and Saved") con.commit()
- When the Edit Button is clicked, this function will be evoked.
- get() – Using the get() method the title name and text entered.
- Then using the if else loop we display different Message Boxes. If the title or text area is empty, we will display the message that details need to be entered.
- When the details are entered correctly, the Edit Button will update the already existing notes by changing the content of the note.
- Execute() – we execute the query of updating the note.
6. View Button Functionality:
#Display all the notes def view_notes(): #Obtain all the user input notes_title = Note_Title_Entry.get() #If no input is given, retrieve all notes if(len(notes_title)<=0): sql_statement = "SELECT * FROM notes_table" #Retrieve notes matching a title elif (len(notes_title)>0): sql_statement = "SELECT * FROM notes_table where notes_title ='%s'" %notes_title #Execute the query cur.execute(sql_statement) #Obtain all the contents of the query row = cur.fetchall() #Check if none was retrieved if len(row)<=0: messagebox.showerror(message="Note not Found") else: #Print the notes for i in row: messagebox.showinfo(message=+"\nTitle: "+i[0]+"\nNotes: "+i[1])
- get() – Using the get() method, we will get the title of the note and using this title we try to fetch the note from the database.
- If the note title is found, then we will display it using the query. If it is not found then we display a message box with an error message.
- We can also view the details of all the notes using the fetchall() method.
7. Delete Button Functionality:
#Delete the notes def delete_notes(): #Obtain input values notes_title = Note_Title_Entry.get() #Ask if user wants to delete all notes choice = messagebox.askquestion(message="Do you want to delete all notes?") #If yes is selected, delete all if choice == 'yes': sql_statement = "DELETE FROM notes_table" else: #Delete notes matching a particular date and title if (len(notes_title)<=0): #Raise error for no inputs messagebox.showerror(message = "Enter Details:" ) return else: sql_statement = "DELETE FROM notes_table where date ='%s' and notes_title ='%s'" %( notes_title) #Execute the query cur.execute(sql_statement) messagebox.showinfo(message="Note(s) Deleted") con.commit()
- Whenever we clcik the Delete Button, this function is evoked.
- get() – We get the title using this method.
- Messagebox.askquestion – Using this we display a pop up asking whether we want to delete all notes. If the answer is yes, we execute the query to delete all the notes that exist.
- If not, we delete the note for which we have the title details entered. After the notes are deleted, a pop us using the messagebox is displayed showing that the notes have been deleted
Python Pin Your Notes Output
Summary
We have successfully completed our Sticky Notes Project in Python. To make this project we have used two libraries –
- Tkinter – for creating an easy GUI in Python.
- Sqlite3 – for creating a database and saving the notes.
We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google | Facebook
File “n:\Aprendendo_Udemy\Curso_Python\Curso_Python\Insta_postNote.py”, line 58, in view_notes
messagebox.showinfo(message=+”\nTitle: “+i[0]+”\nNotes: “+i[1])
TypeError: bad operand type for unary +: ‘str’