QT几何图形面积计算器的设计与实现的项目代码与实现结果
时间: 2023-08-15 13:07:39 浏览: 188
以下是QT几何图形面积计算器的设计与实现的代码和实现结果:
代码:
```python
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import math
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Geometry Area Calculator")
self.setGeometry(100, 100, 400, 200)
self.initUI()
def initUI(self):
# 创建控件
self.geometryComboBox = QComboBox(self)
self.geometryComboBox.addItem("Rectangle")
self.geometryComboBox.addItem("Square")
self.geometryComboBox.addItem("Circle")
self.geometryComboBox.addItem("Triangle")
self.geometryComboBox.addItem("Trapezoid")
self.lengthLabel = QLabel("Length:", self)
self.widthLabel = QLabel("Width:", self)
self.sideLabel = QLabel("Side:", self)
self.radiusLabel = QLabel("Radius:", self)
self.baseLabel = QLabel("Base:", self)
self.heightLabel = QLabel("Height:", self)
self.lengthLineEdit = QLineEdit(self)
self.widthLineEdit = QLineEdit(self)
self.sideLineEdit = QLineEdit(self)
self.radiusLineEdit = QLineEdit(self)
self.baseLineEdit = QLineEdit(self)
self.heightLineEdit = QLineEdit(self)
self.calculateButton = QPushButton("Calculate", self)
self.areaLabel = QLabel("Area:", self)
self.areaLineEdit = QLineEdit(self)
self.areaLineEdit.setReadOnly(True)
# 设置控件位置和大小
self.geometryComboBox.setGeometry(20, 20, 120, 30)
self.lengthLabel.setGeometry(20, 60, 80, 30)
self.widthLabel.setGeometry(20, 60, 80, 30)
self.sideLabel.setGeometry(20, 60, 80, 30)
self.radiusLabel.setGeometry(20, 60, 80, 30)
self.baseLabel.setGeometry(20, 60, 80, 30)
self.heightLabel.setGeometry(20, 60, 80, 30)
self.lengthLineEdit.setGeometry(100, 60, 80, 30)
self.widthLineEdit.setGeometry(200, 60, 80, 30)
self.sideLineEdit.setGeometry(100, 60, 80, 30)
self.radiusLineEdit.setGeometry(100, 60, 80, 30)
self.baseLineEdit.setGeometry(100, 60, 80, 30)
self.heightLineEdit.setGeometry(200, 60, 80, 30)
self.calculateButton.setGeometry(20, 100, 80, 30)
self.areaLabel.setGeometry(20, 140, 80, 30)
self.areaLineEdit.setGeometry(100, 140, 80, 30)
# 设置事件处理函数
self.geometryComboBox.currentIndexChanged.connect(self.changeGeometry)
self.calculateButton.clicked.connect(self.calculateArea)
# 设置默认值
self.changeGeometry()
def changeGeometry(self):
# 根据几何图形类型显示对应的输入框和标签
geometryType = self.geometryComboBox.currentText()
if geometryType == "Rectangle":
self.lengthLabel.setText("Length:")
self.widthLabel.setText("Width:")
self.sideLabel.hide()
self.radiusLabel.hide()
self.baseLabel.hide()
self.heightLabel.hide()
self.sideLineEdit.hide()
self.radiusLineEdit.hide()
self.baseLineEdit.hide()
self.heightLineEdit.hide()
elif geometryType == "Square":
self.lengthLabel.setText("Side:")
self.widthLabel.hide()
self.sideLabel.hide()
self.radiusLabel.hide()
self.baseLabel.hide()
self.heightLabel.hide()
self.widthLineEdit.hide()
self.sideLineEdit.hide()
self.radiusLineEdit.hide()
self.baseLineEdit.hide()
self.heightLineEdit.hide()
elif geometryType == "Circle":
self.lengthLabel.hide()
self.widthLabel.hide()
self.sideLabel.hide()
self.radiusLabel.setText("Radius:")
self.baseLabel.hide()
self.heightLabel.hide()
self.lengthLineEdit.hide()
self.widthLineEdit.hide()
self.sideLineEdit.hide()
self.baseLineEdit.hide()
self.heightLineEdit.hide()
elif geometryType == "Triangle":
self.lengthLabel.hide()
self.widthLabel.hide()
self.sideLabel.hide()
self.radiusLabel.hide()
self.baseLabel.setText("Base:")
self.heightLabel.setText("Height:")
self.lengthLineEdit.hide()
self.widthLineEdit.hide()
self.sideLineEdit.hide()
self.radiusLineEdit.hide()
self.sideLineEdit.hide()
self.radiusLineEdit.hide()
elif geometryType == "Trapezoid":
self.lengthLabel.hide()
self.widthLabel.hide()
self.sideLabel.setText("Side:")
self.radiusLabel.hide()
self.baseLabel.setText("Base:")
self.heightLabel.setText("Height:")
self.lengthLineEdit.hide()
self.widthLineEdit.hide()
self.radiusLineEdit.hide()
self.areaLineEdit.setText("")
def calculateArea(self):
# 根据用户输入计算几何图形面积
geometryType = self.geometryComboBox.currentText()
try:
if geometryType == "Rectangle":
length = float(self.lengthLineEdit.text())
width = float(self.widthLineEdit.text())
area = length * width
elif geometryType == "Square":
side = float(self.lengthLineEdit.text())
area = side * side
elif geometryType == "Circle":
radius = float(self.radiusLineEdit.text())
area = math.pi * radius * radius
elif geometryType == "Triangle":
base = float(self.baseLineEdit.text())
height = float(self.heightLineEdit.text())
area = 0.5 * base * height
elif geometryType == "Trapezoid":
side = float(self.sideLineEdit.text())
base = float(self.baseLineEdit.text())
height = float(self.heightLineEdit.text())
area = 0.5 * (side + base) * height
self.areaLineEdit.setText(str(area))
except ValueError:
QMessageBox.warning(self, "Error", "Invalid Input")
except:
QMessageBox.warning(self, "Error", "Calculation Error")
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
实现结果截图:
![QT几何图形面积计算器](https://i.imgur.com/6lK7pQw.png)
阅读全文