Hello, today we will create a simple Python notepad application. In this tutorial, we will build a GUI application using PyQt and cover how to save and load notes through file input and output. Through this, you will enhance your understanding of basic GUI programming and file handling in Python.
1. Python GUI Programming
GUI stands for Graphical User Interface, which is a graphic-based interface that allows users to easily use the program. PyQt is one of the most widely used GUI toolkits in Python, providing powerful and diverse widgets. In this tutorial, we will create a simple notepad using PyQt5.
2. Installing Required Tools
First, we need to install PyQt5. Run the following command to install PyQt5:
pip install PyQt5
3. Setting Up Project Structure
We will set up the basic structure of the project. Create a project folder and create a file for the Python script:
mkdir python-notepad
cd python-notepad
touch notepad.py
Now, let’s start coding.
4. Creating GUI with PyQt5
When using PyQt5, you need to create a window using an instance of QWidget or its derived classes. The necessary elements to create a basic notepad interface are as follows:
- QTextEdit widget for text input
- Functionality to open and save files
- Convenient menu bar
4.1. Writing Basic Code
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QTextEdit, QFileDialog, QMessageBox
from PyQt5.QtGui import QIcon
class Notepad(QMainWindow):
def __init__(self):
super().__init__()
# Create QTextEdit widget
self.textEdit = QTextEdit(self)
self.setCentralWidget(self.textEdit)
# Initial settings for the notepad
self.initUI()
def initUI(self):
# Create menu bar
menubar = self.menuBar()
# Create file menu
fileMenu = menubar.addMenu('File')
# Add open action
openFile = QAction(QIcon('open.png'), 'Open', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('Open file')
openFile.triggered.connect(self.showDialog)
fileMenu.addAction(openFile)
# Add save action
saveFile = QAction(QIcon('save.png'), 'Save', self)
saveFile.setShortcut('Ctrl+S')
saveFile.setStatusTip('Save file')
saveFile.triggered.connect(self.saveFile)
fileMenu.addAction(saveFile)
# Add exit action
exitAction = QAction('Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(self.close)
fileMenu.addAction(exitAction)
# Activate status bar
self.statusBar()
# Set up main window
self.setGeometry(300, 300, 600, 400)
self.setWindowTitle('Notepad')
self.show()
def showDialog(self):
fname, _ = QFileDialog.getOpenFileName(self, 'Open File', '/', "Text files (*.txt)")
if fname:
with open(fname, 'r', encoding='utf-8') as f:
self.textEdit.setText(f.read())
def saveFile(self):
fname, _ = QFileDialog.getSaveFileName(self, 'Save File', '/', "Text files (*.txt)")
if fname:
with open(fname, 'w', encoding='utf-8') as f:
f.write(self.textEdit.toPlainText())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Notepad()
sys.exit(app.exec_())
5. Code Analysis
The above code is a very basic PyQt5 application. With this code, you can implement a simple notepad feature that allows you to open, edit, and save files. Each part is composed as follows:
5.1. Notepad Class
The Notepad class inherits from the QMainWindow class and is responsible for the main functionality of the window in the PyQt application. Here, we placed the QTextEdit widget in the center and added functionality to open and save files through the menu bar.
5.2. initUI Method
This handles the initial UI setup of the notepad, creating the menu bar and status bar, and adding actions such as open, save, and exit to the menu. Each action is set up to call a specific method in response to user interaction.
5.3. showDialog and saveFile Methods
The showDialog method reads the file selected by the user through the open file dialog and displays it in QTextEdit. The saveFile method saves the current text inputted in QTextEdit to the path specified by the user through the save file dialog.
6. Conclusion
A basic Python notepad was created using PyQt5. This program implements simple functionality to open, edit, and save files. Through this, you learned the basic usage of PyQt5 and how to develop GUI applications using Python. With this basic notepad example, you will be able to create more complex and feature-rich applications. Be sure to enhance your skills through various practices!
Thank you!