Current location - Education and Training Encyclopedia - Graduation thesis - Python_PyQt5 primer series 1: Reading and writing line editing (background calculation)
Python_PyQt5 primer series 1: Reading and writing line editing (background calculation)
Take the realization of calculating the distance between two points at any two latitudes and longitudes as an example.

Screenshot generated by the program:

# The following is the creation of GUI graphical interface.

Import QtCore, QtGui and QtWidgets from pyqt 5.

From mathimport *

Ui_MainWindow class (object):

Def setupUi (itself, main window):

main window . set object name(" main window ")

Main window. resize (800,599)

self.centralwidget = QtWidgets。 QWidget (main window)

self . central widget . set object name(" central widget ")

self.label_2 = QtWidgets。 QLabel(self.centralwidget)

self . label _ 2 . set geometry(Qt core。 QRect(70,90,4 1, 16))

self . label _ 2 . set object name(" label _ 2 ")

self.label_5 = QtWidgets。 QLabel(self.centralwidget)

self . label _ 5 . set geometry(Qt core。 QRect(70, 150,4 1,9))

self . label _ 5 . set object name(" label _ 5 ")

self.label_6 = QtWidgets。 QLabel(self.centralwidget)

self . label _ 6 . set geometry(Qt core。 QRect(50,230,4 1,9))

self . label _ 6 . set object name(" label _ 6 ")

self.label_3 = QtWidgets。 QLabel(self.centralwidget)

self . label _ 3 . set geometry(Qt core。 QRect(270,90,4 1, 16))

self . label _ 3 . set object name(" label _ 3 ")

self.label_7 = QtWidgets。 QLabel(self.centralwidget)

self . label _ 7 . set geometry(Qt core。 QRect(270, 150,4 1,9))

self . label _ 7 . set object name(" label _ 7 ")

self.lineEdit = QtWidgets。 QLineEdit(self.centralwidget)

self . line edit . set geometry(Qt core。 QRect(50, 1 10,7 1,3 1))

self . line edit . set object name(" line edit ")

self.lineEdit_2 = QtWidgets。 QLineEdit(self.centralwidget)

self . line edit _ 2 . set geometry(Qt core。 QRect(50, 170,7 1,3 1))

self . line edit _ 2 . set object name(" line edit _ 2 ")

self.lineEdit_3 = QtWidgets。 QLineEdit(self.centralwidget)

self . line edit _ 3 . set geometry(Qt core。 QRect(250, 1 10,7 1,3 1))

self . line edit _ 3 . set object name(" line edit _ 3 ")

self.lineEdit_4 = QtWidgets。 QLineEdit(self.centralwidget)

self . line edit _ 4 . set geometry(Qt core。 QRect(250, 170,7 1,3 1))

self . line edit _ 4 . set object name(" line edit _ 4 ")

self.lineEdit_5 = QtWidgets。 QLineEdit(self.centralwidget)

self . line edit _ 5 . set geometry(Qt core。 QRect(80,220,7 1,3 1))

self . line edit _ 5 . set object name(" line edit _ 5 ")

self.pushButton = QtWidgets。 Q button (self.centralwidget)

self . button . set geometry(Qt core。 QRect( 170,300,56, 17))

self . push button . set object name(" push button ")

main window . setcentralwidget(self . centralwidget)

self.menubar = QtWidgets。 Menu bar (main window)

self . menubar . set geometry(Qt core。 QRect(0,0,800, 18))

self . menubar . set object name(" menubar ")

main window . setmenubar(self . menubar)

self.statusbar = QtWidgets。 QStatusBar (main window)

self . status bar . set object name(" status bar ")

main window . setstatus bar(self . status bar)

Self.retranslateUi (main window)

self . push button . clicked . connect(self . btnshanbing _ click)

QtCore。 QMetaObject.connectSlotsByName (main window)

Define the retranslation interface (itself, main window):

_translate = QtCore。 QCoreApplication.translate

MainWindow . setwindowtitle(_ translate(" main window "," main window "))

Self.label _ 2.settext (_ translate ("main window", "longitude"))

Self.label _ 5.settext (_ translate ("main window", "longitude"))

Self.label _ 6.settext (_ translate ("main window", "distance"))

Self.label _ 3.settext (_ translate ("main window", "longitude"))

Self.label _ 7.settext (_ translate ("main window", "longitude"))

Self.pushbutton.settext (_ translate ("main window", "OK"))

# The following is the slot function of btnshanbing_click signal.

Def btnshanbing_click (self):

# The following is to read the contents of the text box and convert the type inside to floating-point type.

a=float(self.lineEdit.text())

c=float(self.lineEdit_2.text())

b=float(self.lineEdit_3.text())

d=float(self.lineEdit_4.text())

# The following is the longitude and latitude calculation formula

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))* 637 104

# The following is the calculation result generated in LineEdit_5, but it should be noted that the calculation result must be converted into character type, otherwise the program will not run.

self.lineEdit_5.setText(str(e))

Main program:

Self.lineEdit.text (): read the contents of the text box, but it must be converted into float(self.lineEdit.text ()) before calculation can be made, otherwise calculation cannot be made.

Self.lineEdit_5.setText(str ()): The calculation result generated in another text box must be converted to character type, otherwise the program will report an error.

To sum up: the return type of lineEdit.text () is text.