Here is an example of how you can create a simple notepad app using the Tkinter library in Python:
import tkinter as tk
from tkinter import filedialog
def open_file():
filepath = filedialog.askopenfilename()
with open(filepath, 'r') as f:
text.delete('1.0', tk.END)
text.insert('1.0', f.read())
def save_file():
filepath = filedialog.asksaveasfilename()
with open(filepath, 'w') as f:
f.write(text.get('1.0', tk.END))
root = tk.Tk()
root.title("Simple Notepad")
text = tk.Text(root)
text.pack()
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar)
filemenu.add_command(label="Open", command=open_file)
filemenu.add_command(label="Save", command=save_file)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
root.mainloop()
This code creates a simple notepad app that allows you to open and save text files. The open_file()
function uses the filedialog.askopenfilename()
method to open a file explorer window and allows the user to select a file to open. The selected file is then opened and its contents are displayed in the text widget. The save_file()
function uses the filedialog.asksaveasfilename()
method to open a file explorer window and allows the user to select a location and filename to save the contents of the text widget.
The above code creates a simple notepad app using the Tkinter library in Python. It starts by importing the necessary modules, tkinter
and filedialog
. The filedialog
module is used to create file explorer windows that allow the user to open and save files.
The open_file()
function is defined first. This function is called when the user selects “Open” from the File menu. The filedialog.askopenfilename()
method is used to open a file explorer window and allows the user to select a file to open. Once the user has selected a file, it is opened using the open()
function and the contents are read using the read()
method. The text.delete('1.0', tk.END)
method is used to clear the contents of the text widget, and the text.insert('1.0', f.read())
method is used to insert the contents of the selected file into the text widget, starting at the position ‘1.0’.
The save_file()
function is defined next. This function is called when the user selects “Save” from the File menu. The filedialog.asksaveasfilename()
method is used to open a file explorer window and allows the user to select a location and filename to save the contents of the text widget. Once the user has selected a location and filename, the open()
function is used to create a new file and the write()
method is used to write the contents of the text widget to the new file.
The root
variable is set to an instance of the Tk()
class, which creates the main window of the app. The title("Simple Notepad")
method sets the title of the window to “Simple Notepad”. The text
variable is set to an instance of the Text()
class, which creates a text widget in the main window. The pack()
method is used to add the text widget to the main window.
The menubar
variable is set to an instance of the Menu()
class, which creates a menu bar at the top of the main window. The filemenu
variable is set to an instance of the Menu()
class, which creates a menu labeled “File” in the menu bar. The add_command()
method is used to add “Open” and “Save” options to the “File” menu, and the add_cascade()
method is used to link the “File” menu to the menu bar. The root.config(menu=menubar)
method is used to add the menu bar to the main window.
Finally, the root.mainloop()
method is used to start the main event loop of the app, which runs indefinitely until the user closes the window.

It’s important to note that, the above code creates a simple notepad app that allows you to open and save text files, but it’s not a fully featured notepad. You can add more functionality and features to your notepad app like adding Cut, Copy, Paste functionality, adding font, color, font size and many more.