赞
踩
1、打开QTdesigner
2、选择Dialog without Buttons,并单击“创建”按钮。
3、添加控件。
(1)拖拽“Label”控件,在文本属性中添加文字“请输入身高(cm):”;在Label水平位置拖拽一个“Line Edit”控件。
(2)拖拽“Label”控件,在文本属性中添加文字“请输入体重(kg):”;在Label水平位置拖拽一个“Line Edit”控件。
(3)脱宅“Label”控件,在文本属性中添加文字“提示:1、中国成人居民BMI衡量标准是小于等于18.4为消瘦,18.5-23.9为正常,24-27.9为超重,大于等于28为肥胖。2、最理想的BMI数值为22,理想体重=22*身高^2(米),±10%波动。”,并勾选“wordWrap”属性(换行)。效果如图所示。
(4)拖拽“Push Button”按钮控件,在文本属性中添加文字“计算BMI”。
(5)拖拽“Label”控件,在文本属性中添加文字“BMI:27.18,属于超重。您的理想体重为:68kg”。并勾选“wordWrap”属性(换行)。
(6)现在界面如图:
(7)“ctrl+s”保存文件。文件名称为“bmi_compute.ui”。
1、框选下图所示控件,点击“水平布局”,进行水平布局。
2、框选下图所示控件,点击“水平布局”(快捷键为“ctrl+1”),进行水平布局。
3、框选下图所示控件,点击“水平布局”(快捷键为“ctrl+2”),进行垂直布局。
4、框选下图所示控件,点击“水平布局”(快捷键为“ctrl+1”),进行水平布局。
5、增加“Label”控件,如下图。框选下图所示控件,点击“水平布局”(快捷键为“ctrl+2”),进行垂直布局。
6、单击窗体空白处,右键点击“垂直布局”(快捷键为“ctrl+2”),对整个窗体进行垂直布局。如下图所示:
7、选择窗体的"layoutstetch"属性,将上下部分的比例改为8:2,如下图所示。
8、选中下图所示控件,选择控件的"layoutstetch"属性,将左右部分的比例改为8:2,如下图所示。
9、已完成整个窗体布局,“ctrl+s”进行保存。
1、右键单击窗体,选择“改变样式表”,修改控件的样式。如下图所示:
2、在“编辑样式表”中输入下述指令:
*{
fontsize:14px;
}
QPushButton{
height:50px;
}
QLabel{
color:blue;
}
如图所示:
3、右键单击如图所示控件,打开“改变样式表”,输入下列代码,改变控件的背景色:
*{
background-color:rgb(170, 255, 127);
}
4、分别添加以下两个控件的“改变样式表”。
输入以下代码:
*{
background-color:rgb(255, 255, 0);
}
效果如图:
5、选择按钮控件,选择“改变样式表”,如下图所示:
输入代码,使按钮字体加粗:
QPushButton{
font:bold;
}
6、“ctrl+s”进行文件保存。
1、选择下图所示控件,将"objectName"属性变为“lineEdit_height”。
2、选择下图所示控件,将"objectName"属性变为“lineEdit_weight”。
3、选择下图所示控件,将"objectName"属性变为“label_result”。
4、选择窗体,将"objectName"属性变为“BmiCompute”。
5、选择窗体,将"windowTitle"属性变为“人体BMI计算器”。
6、在anaconda中将“bmi_compute.ui”文件变为“bmi_compute.py”。如下图所示:
7、生成的“bmi_compute.py”代码如下:
# Form implementation generated from reading ui file 'bmi_compute.ui' # # Created by: PyQt6 UI code generator 6.4.0 # # WARNING: Any manual changes made to this file will be lost when pyuic6 is # run again. Do not edit this file unless you know what you are doing. from PyQt6 import QtCore, QtGui, QtWidgets class Ui_BmiCompute(object): def setupUi(self, BmiCompute): BmiCompute.setObjectName("BmiCompute") BmiCompute.resize(562, 417) BmiCompute.setStyleSheet("*{\n" " fontsize:14px;\n" "}\n" "QPushButton{\n" " height:50px;\n" "}\n" "QLabel{\n" " color:blue;\n" "}") self.verticalLayout_3 = QtWidgets.QVBoxLayout(BmiCompute) self.verticalLayout_3.setObjectName("verticalLayout_3") self.horizontalLayout_3 = QtWidgets.QHBoxLayout() self.horizontalLayout_3.setObjectName("horizontalLayout_3") self.verticalLayout = QtWidgets.QVBoxLayout() self.verticalLayout.setObjectName("verticalLayout") self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.label = QtWidgets.QLabel(BmiCompute) self.label.setObjectName("label") self.horizontalLayout.addWidget(self.label) self.lineEdit_height = QtWidgets.QLineEdit(BmiCompute) self.lineEdit_height.setObjectName("lineEdit_height") self.horizontalLayout.addWidget(self.lineEdit_height) self.verticalLayout.addLayout(self.horizontalLayout) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.label_2 = QtWidgets.QLabel(BmiCompute) self.label_2.setObjectName("label_2") self.horizontalLayout_2.addWidget(self.label_2) self.lineEdit_weight = QtWidgets.QLineEdit(BmiCompute) self.lineEdit_weight.setObjectName("lineEdit_weight") self.horizontalLayout_2.addWidget(self.lineEdit_weight) self.verticalLayout.addLayout(self.horizontalLayout_2) self.pushButton = QtWidgets.QPushButton(BmiCompute) self.pushButton.setStyleSheet("QPushButton{\n" " font:bold;\n" "}\n" "") self.pushButton.setObjectName("pushButton") self.verticalLayout.addWidget(self.pushButton) self.horizontalLayout_3.addLayout(self.verticalLayout) self.label_3 = QtWidgets.QLabel(BmiCompute) self.label_3.setStyleSheet("*{\n" " background-color:rgb(170, 255, 127);\n" "}") self.label_3.setWordWrap(True) self.label_3.setObjectName("label_3") self.horizontalLayout_3.addWidget(self.label_3) self.horizontalLayout_3.setStretch(0, 8) self.horizontalLayout_3.setStretch(1, 2) self.verticalLayout_3.addLayout(self.horizontalLayout_3) self.verticalLayout_2 = QtWidgets.QVBoxLayout() self.verticalLayout_2.setObjectName("verticalLayout_2") self.label_5 = QtWidgets.QLabel(BmiCompute) self.label_5.setStyleSheet("*{\n" " background-color:rgb(255, 255, 0);\n" "}") self.label_5.setWordWrap(True) self.label_5.setObjectName("label_5") self.verticalLayout_2.addWidget(self.label_5) self.label_result = QtWidgets.QLabel(BmiCompute) self.label_result.setStyleSheet("*{\n" " background-color:rgb(255, 255, 0);\n" "}") self.label_result.setWordWrap(True) self.label_result.setObjectName("label_result") self.verticalLayout_2.addWidget(self.label_result) self.verticalLayout_3.addLayout(self.verticalLayout_2) self.verticalLayout_3.setStretch(0, 8) self.verticalLayout_3.setStretch(1, 2) self.retranslateUi(BmiCompute) QtCore.QMetaObject.connectSlotsByName(BmiCompute) def retranslateUi(self, BmiCompute): _translate = QtCore.QCoreApplication.translate BmiCompute.setWindowTitle(_translate("BmiCompute", "人体BMI计算器")) self.label.setText(_translate("BmiCompute", "请输入身高(cm):")) self.label_2.setText(_translate("BmiCompute", "请输入体重(kg):")) self.pushButton.setText(_translate("BmiCompute", "计算BMI")) self.label_3.setText(_translate("BmiCompute", "提示:1、中国成人居民BMI衡量标准是小于等于18.4为消瘦,18.5-23.9为正常,24-27.9为超重,大于等于28为肥胖。2、最理想的BMI数值为22,理想体重=22*身高^2(米),±10%波动。")) self.label_5.setText(_translate("BmiCompute", "您的计算结果是:")) self.label_result.setText(_translate("BmiCompute", "BMI:27.18,属于超重。您的理想体重为:68kg"))
项目结构如下:
8、在“bmi_compute.py”文件同级建立“bmi_compute_main.py”文件。代码如下:
#导入sys模块 import sys #导入PyQt6模块 from PyQt6.QtWidgets import ( QApplication,QDialog,QMessageBox ) #引入同级的文件“bmi_compute" import bmi_compute #创建MyBmiCompute类 class MyBmiCompute(bmi_compute.Ui_BmiCompute,QDialog): #创建构造函数 def __init__(self): #引入bmi_compute.Ui_BmiCompute父类 super().__init__() #调用bmi_compute.Ui_BmiComput父类参数setupUi self.setupUi(self) #显示窗口 self.show() #绑定按钮调用的事件函数 self.pushButton.clicked.connect(self.compute_bmi) #构建compute_bmi函数 def compute_bmi(self): #将lineEdit_height的文本属性赋值给heitht height=self.lineEdit_height.text() #判断身高的输入值是否为小数。如果不是,弹出消息框 try: height=float(height)/100 except: QMessageBox.warning(self,"信息提示","你输入的身高不是小数,请重新输入") return # 将lineEdit_weight的文本属性赋值给weitht weight=self.lineEdit_weight.text() #判断体重输入的值是否为小数。如果不是,弹出消息框 try: weight=float(weight) except: QMessageBox.warning(self,"信息提示","你输入的体重不是小数,请重新输入") return #保存计算结果保留2位小数 result=round(weight/(height**2),2) #理想的BMI进行计算,并保留2位小数 ideal_result=round(22*(height**2),2) #将计算结果输入到输出标签中 result_text=f"BMI:{result},属于超重。您的理想体重为:{ideal_result}kg" #将计算结果转行成字符串,赋值给label_result控件 self.label_result.setText(str(result_text)) #弹出信息框,说明计算成功 QMessageBox.warning(self, "信息提示", "计算成功") #创建入口函数 if __name__ == '__main__': # 入口函数 app=QApplication(sys.argv) myBmiCompute=MyBmiCompute() # 退出系统 sys.exit(app.exec())
1、在anaconda终端输入以下命令:
pyinstaller -F -w bmi_compute_main.py
对文件进行打包。如图;
2、打包后的文件如图所示:
本文通过一个计算BMI的例子,进一步演示PyQt6的简单应用。希望对新手学习者有帮助。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。