Level 2: Lab 1 with PyQt

Posted by


In this tutorial, we will be focusing on creating a simple GUI application using PyQt. PyQt is a set of Python bindings for the Qt application framework developed by Riverbank Computing. It allows you to create cross-platform graphical applications using Python.

For this tutorial, we will be creating a basic calculator application with a simple user interface. We will use PyQt5, which is the latest version of PyQt. Make sure you have PyQt5 installed on your system before proceeding with this tutorial.

To create a PyQt application, you will need to create a new Python script and import the necessary modules. Start by creating a new Python script and import the following modules:

from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLineEdit

Next, create a class for your main window by subclassing QWidget:

class CalculatorApp(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setWindowTitle('Simple Calculator')
        self.setGeometry(100, 100, 300, 200)

        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        self.result_display = QLineEdit()
        self.layout.addWidget(self.result_display)

        buttons_layout = QVBoxLayout()
        self.layout.addLayout(buttons_layout)

        buttons_data = [
            ('7', '8', '9', '/'),
            ('4', '5', '6', '*'),
            ('1', '2', '3', '-'),
            ('C', '0', '=', '+'),
        ]

        for row in buttons_data:
            row_layout = QHBoxLayout()
            for button_text in row:
                button = QPushButton(button_text)
                button.clicked.connect(self.buttonClicked)
                row_layout.addWidget(button)
            buttons_layout.addLayout(row_layout)

    def buttonClicked(self):
        button = self.sender()
        current_text = self.result_display.text()

        if button.text() == 'C':
            self.result_display.clear()
        elif button.text() == '=':
            try:
                result = eval(current_text)
                self.result_display.setText(str(result))
            except Exception as e:
                self.result_display.setText('Error')
        else:
            self.result_display.setText(current_text + button.text())

In the above code, we have created a simple calculator application with a QLineEdit widget for displaying the result and QPushButton widgets for buttons. The buttons call the buttonClicked method when clicked, which updates the text in the result_display field accordingly.

Finally, instantiate the QApplication and CalculatorApp classes to create and display the application window:

if __name__ == '__main__':
    app = QApplication([])
    win = CalculatorApp()
    win.show()
    app.exec_()

Run the script, and you should see a window with a simple calculator interface. You can enter numbers and operators using the buttons, and the result will be displayed in the QLineEdit field.

This concludes our tutorial on creating a simple calculator application using PyQt. You can further customize the application by adding more functionality or improving the design. PyQt offers a wide range of widgets and features that you can explore to create more advanced GUI applications.

0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x