Today Let's see how you can build your own notepad in Python using the Tkinter Module and later we'll even convert this program into a .exe executable so that we run it as an application and do not need to run it again and again from the terminal or an ide.
Let's begin, First import all the necessary modules as follows:
from tkinter import * from tkinter.filedialog import asksaveasfilename, askopenfilename from tkinter.messagebox import * import os
For now import all these modules as we'll be needing them later in this article.
The tkinter module has all the necessary modules and methods required pre made and ready to use. This is something I like about coding in Python you have a library for each and everything which speeds up the development a lot.
Let's first see how to make a simple window in Tkinter:
# Creating a window for our application. window = Tk() window.mainloop()
This is a simple window in tkinter you can play around it and if you are new to tkinter you can change somethings try running the code without the mainloop() and see what happens.
Did nothing appear on your screen ? This is because when we are running a gui we want our application to run infinitely and perform certain tasks until the window is manually closed or exited.
Now this is simple plain window let's start tweaking it a bit according to our preferances.
You can change the title of you window easily by using window.title() as follows:
# Defining the title of our Window. window.title('Notepad')
Now the next thing that we need is a Text Box in which we can type something otherwise what's the use of our text editor !!
You can simply create a Text as follows:
# Creating a variable for the text inputs so that later on we can retrieve the data from it. editor = Text() # Now You need to place this input Text box on your window. editor.pack(expand= True, fill= 'both')
The expand argument is set to True and we are filling the text box widget in both the directions i.e horizontally as well as vertically. This is done because We want our text input widget to span the entire window so when we minize or maximize or resize our window it scales accordingly.
You can first try by just doing editor.pack() and then try to resize your window and see the changes in this way you'll learn what is going on and why we are doing such things.
Now, we have a simple window with out title and in this window we can type anything anywhere within the window.
Now, Let's create a menubar as a typical notepad has one:
# Defining a MenuBar. In Menu pass in the name of your window mine happens to be window (Which we declared earlier window = Tk() ). menu_bar = Menu(window) # Creating a File Menu for our menubar. # For the first time just run this code without the tearoff= False and observe what you get. file_menu = Menu(menu_bar, tearoff= False) # Adding commands to our file_menu. # Note: the open file is a function just pass the function without the parenethesis so whenever you choose that option from the file menu that action will be performed. file_menu.add_command(label= 'open', command= open_file) # Now, Let's add a cascade to our menubar. menu_bar.add_cascade(label= 'File', menu= file_menu) # Now our menubar is ready but wait we didn't tell the window that hey we have a menubar. So let's do that. window.config(menu = menu_bar)
Now, as we are done with that let's add some commands for our open option.
To open a File dialog box as we have in our native windows notepad app we are using tkinter.filedialog as follows:
def open_file(): path = askopenfilename() # Now since we have the path of the file that we want to open let's import it's contents in our Notepad app. with open(path, 'r') as file: text = file.read() # We have the contents of that file let's displat it on our window. # First delete all the existing contents from your window so that we can load the stuff fresh editor.delete('1.0', END) editor.insert('1.0', text)
Here, we go it successfully opened a file Dialog and opened that file for us into our text editor !!
Also, Yoy may have noticed that a traditional Notepad has keybindings for tasks such as 'Ctrl + s' to save the work and so on so let's add that functionality to our App.
# Applying keybindings to our app. # Also note that if you want to apply keybindings to a function you need to pass an argument named event to that functions as follows: def open_file(event): path = askopenfilename() # Now since we have the path of the file that we want to open let's import it's contents in our Notepad app. with open(path, 'r') as file: text = file.read() # We have the contents of that file let's displat it on our window. # First delete all the existing contents from your window so that we can load the stuff fresh editor.delete('1.0', END) editor.insert('1.0', text) window.bind('<Control-o>', open_file) # This will bind the keys 'ctrl + o' to our open file function.
This was Basic Overview of the things which I tried to explain in Depth so that the rest of the parts can be easily understood.
Also, the best way to build something is to make mistakes but learn from them and grow.
I can explain each and every function here but that will be way too lengthy and time consuming also if I do so you'll know only limited stuff about the topics which I cover. Instead, I'll leave my code below for you'll to go through and then you'll can implement those features in your application also you can even add more and better features than me if you opt to do it that way and the amount of things that you,ll learn will be much higher.
You can check out the entire code in my Github account I have implemented a lot of functionalities trying it to keep it as identical as we have in our native windows.
Also below that I have attached a replit Notebook where you can run my code and the output here it self.
Thankyou for reading my post Hope You liked it :)