前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >PyQt5: BMI指数计算

PyQt5: BMI指数计算

作者头像
Exploring
发布2025-03-24 16:30:23
发布2025-03-24 16:30:23
420
举报

文章背景:最近在使用PyQt5来创建一些带有UI界面的小工具。下面制作一个简单的界面来计算BMI指数。

系统: Win

代码编辑器:PyCharm Community Edition 2024.3.4

Python版本:Python37

操作步骤:先通过QT designer来设计界面,得到bmi_ui.ui文件,然后再通过pyuic转化为bmi_ui.py文件。然后再创建主程序,导入ui模型。

文件如下:

UI界面如下:

Python主程序如下:

from PyQt5.QtCore import pyqtSlot

from PyQt5.QtWidgets import QMessageBox, QMainWindow, QApplication

from bmi_ui import Ui_MainWindow

import sys

class Example(QMainWindow):

def __init__(self):

super().__init__()

self.ui = Ui_MainWindow()

self.ui.setupUi(self)

self.ui.pushButton.clicked.connect(self.onClick)

@pyqtSlot()

def onClick(self):

if self.ui.lineEdit_Length.text() == '' or self.ui.lineEdit_Weight.text() == '':

QMessageBox.about(self, "BMI", "Type something")

else:

height = float(self.ui.lineEdit_Length.text())

mass = float(self.ui.lineEdit_Weight.text())

bmi = mass / (height * height)

bmi = round(bmi, 2)

self.ui.Label_Output.setText(str(bmi))

# ======================

# main

# ======================

if __name__ == '__main__':

app = QApplication([])

win = Example()

win.show()

sys.exit(app.exec())

参考资料:

[1] [Create Desktop Apps with Python PyQt5](https://gumroad.com/d/68fd9e485e55e499552f04dff0996835)

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-03-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据处理与编程实践 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档