실습: PyQt6를 사용한 BMI 계산기(GUI) – LLM Python 11-1

Posted by

<!DOCTYPE html>

LLM파이썬11-1 PyQt6를 활용한 GUI 실습(BMI계산기)

LLM파이썬11-1 PyQt6를 활용한 GUI 실습(BMI계산기)

In this tutorial, we will create a simple GUI application using PyQt6 to calculate Body Mass Index (BMI).

Step 1: Installing PyQt6

Before we start, you need to ensure that PyQt6 is installed on your system. You can install it using pip:


pip install PyQt6

Step 2: Creating the GUI

Now, let’s create the GUI for our BMI calculator:


import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton

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

self.initUI()

def initUI(self):
self.setGeometry(100, 100, 400, 200)
self.setWindowTitle('BMI Calculator')

self.weight_label = QLabel('Weight (kg):', self)
self.weight_label.move(50, 50)

self.weight_input = QLineEdit(self)
self.weight_input.move(150, 50)

self.height_label = QLabel('Height (cm):', self)
self.height_label.move(50, 80)

self.height_input = QLineEdit(self)
self.height_input.move(150, 80)

self.calculate_button = QPushButton('Calculate BMI', self)
self.calculate_button.move(100, 120)
self.calculate_button.clicked.connect(self.calculate_bmi)

self.result_label = QLabel(self)
self.result_label.move(50, 150)

def calculate_bmi(self):
weight = float(self.weight_input.text())
height = float(self.height_input.text()) / 100

bmi = weight / (height ** 2)

self.result_label.setText('Your BMI is: {:.2f}'.format(bmi))

if __name__ == '__main__':
app = QApplication(sys.argv)
calculator = BMI_Calculator()
calculator.show()
sys.exit(app.exec())

Step 3: Running the Application

Save the above code in a file named bmi_calculator.py. Now, you can run the application by executing the following command:


python bmi_calculator.py

Once the application is running, you can enter your weight and height in the respective fields and click on the Calculate BMI button to get your Body Mass Index.

That’s it! You have successfully created a simple BMI calculator using PyQt6.