用Python编写关于计算图形面积的代码实现抛弃控制台程序实现用界面窗口来实现
作者:互联网
用Python编写关于计算图形面积的代码实现抛弃控制台程序实现用界面窗口来实现
界面是用Python + qt5 来实现的由于代码简单就窗口函数和功能函数没有分开写直接写在了untitled.py文件中,代码如下:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QSize
from PyQt5.QtWidgets import QLineEdit, QMessageBox
from figure import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(477, 551)
MainWindow.setMinimumSize(QSize(477, 551))
MainWindow.setMaximumSize(QSize(477, 551))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(100, 20, 151, 41))
self.label.setObjectName("label")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(250, 30, 111, 31))
self.comboBox.setObjectName("comboBox")
self.comboBox.activated.connect(self.change)
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(80, 141, 71, 20))
self.label_2.setObjectName("label_2")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(80, 180, 71, 20))
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(80, 220, 71, 20))
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(80, 260, 71, 20))
self.label_6.setObjectName("label_6")
self.label_7 = QtWidgets.QLabel(self.centralwidget)
self.label_7.setGeometry(QtCore.QRect(80, 300, 71, 20))
self.label_7.setObjectName("label_7")
self.label_8 = QtWidgets.QLabel(self.centralwidget)
self.label_8.setGeometry(QtCore.QRect(80, 340, 71, 20))
self.label_8.setObjectName("label_8")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(150, 140, 113, 21))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.setValidator(QtGui.QIntValidator(10000000, 99999999))
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(150, 180, 113, 21))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_2.setValidator(QtGui.QIntValidator(10000000, 99999999))
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(150, 220, 113, 21))
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_3.setValidator(QtGui.QIntValidator(10000000, 99999999))
self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(QtCore.QRect(150, 260, 113, 21))
self.lineEdit_4.setObjectName("lineEdit_4")
self.lineEdit_4.setValidator(QtGui.QIntValidator(10000000, 99999999))
self.lineEdit_5 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_5.setGeometry(QtCore.QRect(150, 300, 113, 21))
self.lineEdit_5.setObjectName("lineEdit_5")
self.lineEdit_5.setValidator(QtGui.QIntValidator(10000000, 99999999))
self.lineEdit_6 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_6.setGeometry(QtCore.QRect(150, 340, 113, 21))
self.lineEdit_6.setObjectName("lineEdit_6")
self.lineEdit_6.setValidator(QtGui.QIntValidator(10000000, 99999999))
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(170, 390, 41, 31))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth())
self.label_3.setSizePolicy(sizePolicy)
self.label_3.setObjectName("label_3")
self.label_9 = QtWidgets.QLabel(self.centralwidget)
self.label_9.setGeometry(QtCore.QRect(170, 440, 41, 31))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_9.sizePolicy().hasHeightForWidth())
self.label_9.setSizePolicy(sizePolicy)
self.label_9.setObjectName("label_9")
self.lineEdit_7 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_7.setGeometry(QtCore.QRect(210, 390, 81, 31))
self.lineEdit_7.setObjectName("lineEdit_7")
self.lineEdit_8 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_8.setGeometry(QtCore.QRect(210, 440, 81, 31))
self.lineEdit_8.setObjectName("lineEdit_8")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)#1
self.pushButton.setGeometry(QtCore.QRect(330, 200, 93, 28))
self.pushButton.clicked.connect(self.operate)
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)#2
self.pushButton_2.setGeometry(QtCore.QRect(330, 290, 93, 28))
self.pushButton_2.clicked.connect(self.renovate)
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget) # 3
self.pushButton_3.setGeometry(QtCore.QRect(0, 0, 60, 40))
self.pushButton_3.clicked.connect(self.info)
self.pushButton_3.setObjectName("pushButton_3")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 477, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "图形面积计算器"))
self.label.setText(_translate("MainWindow", "选择你要计算的图形:"))
self.comboBox.setItemText(0, _translate("MainWindow", "圆形"))
self.comboBox.setItemText(1, _translate("MainWindow", "矩形"))
self.comboBox.setItemText(2, _translate("MainWindow", "三角形"))
self.comboBox.setItemText(3, _translate("MainWindow", "任意四边形"))
self.label_2.setText(_translate("MainWindow", "半 径 R:"))
self.label_4.setText(_translate("MainWindow", " 边 1:"))
self.label_5.setText(_translate("MainWindow", " 边 2:"))
self.label_6.setText(_translate("MainWindow", " 边 3:"))
self.label_7.setText(_translate("MainWindow", " 边 4:"))
self.label_8.setText(_translate("MainWindow", " 角 度:"))
self.label_3.setText(_translate("MainWindow", "面积:"))
self.label_9.setText(_translate("MainWindow", "周长:"))
self.pushButton.setText(_translate("MainWindow", "开始计算"))
self.pushButton_2.setText(_translate("MainWindow", "刷新清零"))
self.pushButton_3.setText(_translate("MainWindow", "关于"))
def operate(self):
if self.comboBox.currentText() == "圆形":
x1 = int(self.lineEdit.text())
self.lineEdit_7.setText(str(Round(x1).area()))
self.lineEdit_8.setText(str(Round(x1).girth()))
elif self.comboBox.currentText() == "矩形":
x1 = int(self.lineEdit_2.text())
x2 = int(self.lineEdit_3.text())
self.lineEdit_7.setText(str(Rectangle(x1, x2).area()))
self.lineEdit_8.setText(str(Rectangle(x1, x2).girth()))
elif self.comboBox.currentText() == "三角形":
x1 = int(self.lineEdit_2.text())
x2 = int(self.lineEdit_3.text())
x3 = int(self.lineEdit_4.text())
self.lineEdit_7.setText(str(Triangle(x1, x2, x3).area()))
self.lineEdit_8.setText(str(Triangle(x1, x2, x3).girth()))
elif self.comboBox.currentText() == "任意四边形":
x1 = int(self.lineEdit_2.text())
x2 = int(self.lineEdit_3.text())
x3 = int(self.lineEdit_4.text())
x4 = int(self.lineEdit_5.text())
x5 = int(self.lineEdit_6.text())
self.lineEdit_7.setText(str(Guadrilateral(x1, x2, x3, x4, x5).area()))
self.lineEdit_8.setText(str(Guadrilateral(x1, x2, x3, x4, x5).girth()))
def change(self):
if self.comboBox.currentText() == "圆形":
self.lineEdit.setReadOnly(0)
self.lineEdit_2.setReadOnly(1)
self.lineEdit_3.setReadOnly(1)
self.lineEdit_4.setReadOnly(1)
self.lineEdit_5.setReadOnly(1)
self.lineEdit_6.setReadOnly(1)
elif self.comboBox.currentText() == "矩形":
self.lineEdit.setReadOnly(1)
self.lineEdit_2.setReadOnly(0)
self.lineEdit_3.setReadOnly(0)
self.lineEdit_4.setReadOnly(1)
self.lineEdit_5.setReadOnly(1)
self.lineEdit_6.setReadOnly(1)
elif self.comboBox.currentText() == "三角形":
self.lineEdit.setReadOnly(1)
self.lineEdit_2.setReadOnly(0)
self.lineEdit_3.setReadOnly(0)
self.lineEdit_4.setReadOnly(0)
self.lineEdit_5.setReadOnly(1)
self.lineEdit_6.setReadOnly(1)
elif self.comboBox.currentText() == "任意四边形":
self.lineEdit.setReadOnly(1)
self.lineEdit_2.setReadOnly(0)
self.lineEdit_3.setReadOnly(0)
self.lineEdit_4.setReadOnly(0)
self.lineEdit_5.setReadOnly(0)
self.lineEdit_6.setReadOnly(0)
def renovate(self):
self.lineEdit.clear()
self.lineEdit_2.clear()
self.lineEdit_3.clear()
self.lineEdit_4.clear()
self.lineEdit_5.clear()
self.lineEdit_6.clear()
self.lineEdit_7.clear()
self.lineEdit_8.clear()
def info(self):
QMessageBox.information(None, '说明', '请计算前先选择你要计算图形的名称,每次计算完成后点击刷新清零按钮', QMessageBox.Ok)
if __name__ == '__main__':
app =QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
下面就是功能函数页面figure.py详细代码如下:
import math
class Round(): #圆形
def __init__(self, R):
self.R = R
def area(self):
return round(self.R * self.R * 3.14, 2)
def girth(self):
return round(self.R * 2 * 3.14, 2)
class Rectangle():#矩形
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
def girth(self):
return (self.length + self.width) * 2
class Triangle():#三角形
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def area(self):
l = (self.a + self.b + self.c)/2
return round(math.sqrt(l * (l - self.a) * (l - self.b) * (l - self.c)), 2) #round保留2为小数
def girth(self):
return self.a + self.b + self.c
class Guadrilateral():#任意四边形
def __init__(self, a, b, c, d, angle):
self.a = a
self.b = b
self.c = c
self.d = d
self.angle = angle
def area(self):
l = (self.a + self.b + self.c + self.d)/2
return round(math.sqrt((l - self.a) * (l - self.b) * (l - self.c) * (l - self.d) -
self.a * self.b * self.c * self.d
* math.pow(math.cos(math.radians(self.angle)), 2)), 2) #pow函数幂次方radians弧度转换为角度
def girth(self):
return self.a + self.b + self.c + self.d
标签:程序实现,QtCore,self,label,Python,控制台,QtWidgets,MainWindow,lineEdit 来源: https://blog.csdn.net/nanxiang11/article/details/112793399