0% found this document useful (0 votes)
26 views

Lab Exercise 8 (Two Number Calculator in PyQT)

This document provides instructions for a lab exercise to create a two-number calculator application using PyQt. Learners will build a graphical user interface (GUI) calculator that can perform addition, subtraction, multiplication, and division on two numbers entered by the user. The instructions guide learners to create a Python script with PyQt code to design the GUI layout with input fields, operation buttons, and an output label. Methods are defined to handle button clicks and perform the arithmetic operations on the input numbers.

Uploaded by

hitesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Lab Exercise 8 (Two Number Calculator in PyQT)

This document provides instructions for a lab exercise to create a two-number calculator application using PyQt. Learners will build a graphical user interface (GUI) calculator that can perform addition, subtraction, multiplication, and division on two numbers entered by the user. The instructions guide learners to create a Python script with PyQt code to design the GUI layout with input fields, operation buttons, and an output label. Methods are defined to handle button clicks and perform the arithmetic operations on the input numbers.

Uploaded by

hitesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Exercise 8 – Two Number Calculator in

PyQT

Lab Exercise: Creating a Two-Number Calculator with PyQt

Creating a lab exercise for building a two-number calculator using PyQt involves
providing a hands-on task for learners to create a simple calculator application in
Python with a graphical user interface. In this exercise, learners will build a basic
calculator that can perform addition, subtraction, multiplication, and division
operations on two numbers entered by the user. Here's a step-by-step lab exercise:

Objective: Create a PyQt application with a graphical user interface for performing
arithmetic operations on two numbers.

Requirements:

 PyQt5 should be installed.

 Basic understanding of Python and PyQt5.

Instructions:

In this lab exercise, learners will create a two-number calculator application using
PyQt. Follow the steps below:

Create the Python Script:

Create a Python script, e.g., calculator.py, and add the following code to create a
basic PyQt application:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout,
QHBoxLayout, QLineEdit, QPushButton, QLabel

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

def initUI(self):
# Create input fields
self.num1_input = QLineEdit(self)
self.num2_input = QLineEdit(self)

# Create buttons for arithmetic operations


self.add_button = QPushButton('+', self)
self.subtract_button = QPushButton('-', self)
self.multiply_button = QPushButton('*', self)
self.divide_button = QPushButton('/', self)

# Create a label to display the result


self.result_label = QLabel('', self)

# Set up layouts
input_layout = QVBoxLayout()
input_layout.addWidget(self.num1_input)
input_layout.addWidget(self.num2_input)

button_layout = QVBoxLayout()
button_layout.addWidget(self.add_button)
button_layout.addWidget(self.subtract_button)
button_layout.addWidget(self.multiply_button)
button_layout.addWidget(self.divide_button)

main_layout = QHBoxLayout()
main_layout.addLayout(input_layout)
main_layout.addLayout(button_layout)
main_layout.addWidget(self.result_label)

self.setLayout(main_layout)
# Connect button clicks to functions
self.add_button.clicked.connect(self.add)
self.subtract_button.clicked.connect(self.subtract)
self.multiply_button.clicked.connect(self.multiply)
self.divide_button.clicked.connect(self.divide)

def add(self):
num1 = float(self.num1_input.text())
num2 = float(self.num2_input.text())
result = num1 + num2
self.result_label.setText(f'Result: {result}')

def subtract(self):
num1 = float(self.num1_input.text())
num2 = float(self.num2_input.text())
result = num1 - num2
self.result_label.setText(f'Result: {result}')

def multiply(self):
num1 = float(self.num1_input.text())
num2 = float(self.num2_input.text())
result = num1 * num2
self.result_label.setText(f'Result: {result}')

def divide(self):
num1 = float(self.num1_input.text())
num2 = float(self.num2_input.text())
if num2 != 0:
result = num1 / num2
self.result_label.setText(f'Result: {result}')
else:
self.result_label.setText('Error: Division by zero')

if __name__ == '__main__':
app = QApplication(sys.argv)
window = CalculatorApp()
window.setWindowTitle('Two-Number Calculator')
window.show()
sys.exit(app.exec_())

Run the Application:

Open a terminal or command prompt, navigate to the directory containing the


Python script (calculator.py), and run the script:
python calculator.py
The PyQt application should open and display a simple two-number calculator.

You might also like