1. Use environment
win10
python3.7.3
PyQt5
Qt Designer
2.ui 设计
(1) Open Qt Designer, create Main Window
(2) Layout
Here, the five words "Exchange Rate Converter" are placed on the label, click to see its name is the name of
the two blank input boxes with the same label The
conversion button for lineEdit and lineEdit_2 is a pushbutton
(3) Generate python code
Use cmd to cut the directory to the folder where this .ui file is located and execute the following command. Please replace the name in the following command with the file name, such as "conversion.py" and "conversion.ui" in this example
pyuic5 -o name.py name.ui
Click Enter to form the "conversion.py" file.
(4) Create the main.py program in the same folder, write the following content, and change the following gui_file_name to conversion
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
import gui_file_name
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = gui_file_name.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Then run main.py in the python compiler to see the interface of your layout, but this interface does not have any function at this time, we need to write something.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QIcon,QFont
from functools import partial
import conversion
def convert(ui):
input = ui.lineEdit.text()
result = float(input) * 6.7
ui.lineEdit_2.setText(str(result))
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = conversion.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
ui.label.setFont(QFont("Microsoft YaHei", 18)) #设置label的字体和大小
ui.pushButton.clicked.connect(partial(convert, ui))
sys.exit(app.exec_())
After doing the above work, you can run this program in main.py.
(5) To generate an executable file (.exe file),
first install pyinstaller
pip install pyinstaller
After the installation is complete, open CMD and then cd to the folder where the program is located, enter
pyinstaller -F -w main.py
Find the dist folder in the folder where the code is located, and the exe software appears in the folder, double-click to open it.
-W in the command means: directly issued exe application with command line debugging window, adding -w command in the command can mask the
command -F means: using -F command can package the application into an independent exe file , otherwise it is a file with all kinds of dll files folder and depend
double-click to open the file
reference 1 .
reference 2 .