通过实现任意两个经纬度计算两点之间的距离为例子
程序生成截图:
#以下是GUI图形界面的创建
from PyQt5import QtCore, QtGui, QtWidgets
from mathimport *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 599)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(70, 90, 41, 16))
self.label_2.setObjectName("label_2")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(70, 150, 41, 9))
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(50, 230, 41, 9))
self.label_6.setObjectName("label_6")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(270, 90, 41, 16))
self.label_3.setObjectName("label_3")
self.label_7 = QtWidgets.QLabel(self.centralwidget)
self.label_7.setGeometry(QtCore.QRect(270, 150, 41, 9))
self.label_7.setObjectName("label_7")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(50, 110, 71, 31))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(50, 170, 71, 31))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(250, 110, 71, 31))
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(QtCore.QRect(250, 170, 71, 31))
self.lineEdit_4.setObjectName("lineEdit_4")
self.lineEdit_5 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_5.setGeometry(QtCore.QRect(80, 220, 71, 31))
self.lineEdit_5.setObjectName("lineEdit_5")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(170, 300, 56, 17))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 18))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.btnshanbing_click)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_2.setText(_translate("MainWindow", "经度"))
self.label_5.setText(_translate("MainWindow", "经度"))
self.label_6.setText(_translate("MainWindow", "距离"))
self.label_3.setText(_translate("MainWindow", "经度"))
self.label_7.setText(_translate("MainWindow", "经度"))
self.pushButton.setText(_translate("MainWindow", "确定"))
#以下是btnshanbing_click信号的槽函数
def btnshanbing_click(self):
#以下是将文本框里面的内容读取出来同时将里面的类型转换为浮点型
a=float(self.lineEdit.text())
c=float(self.lineEdit_2.text())
b=float(self.lineEdit_3.text())
d=float(self.lineEdit_4.text())
#以下是经纬度计算公式
e=(acos(sin(b * pi /180) * sin(d * pi /180) + cos(c * pi /180 - a * pi /180) * cos(b * pi /180) * cos(d * pi /180)))*6371004
#以下是将计算的结果生成在LineEdit_5里面去,但是要注意的是必须将计算的结果转换为字符型,否则程序运行不起来
self.lineEdit_5.setText(str(e))
主程序:
self.lineEdit.text():读取文本框里面的内容,但是要做计算必须转换为float(self.lineEdit.text())浮点型才能做运算,否则不能运算
self.lineEdit_5.setText(str()):将计算的结果生成在另一个文本框里面必须转换为字符型,否则程序会报错。
综上所述:lineEdit.text()的返回类型为文本型