Python&PyQt5 初次使用相关

1、我使用的是Pycharm开发环境,解释器为Python3.4,但在这里面怎么也加载不了PyQt5这个模块,所以我选择了用Qt_Designer做好GUI后,保存为.ui的文件格式,然后通过配置(这里通过在Pycharm里配置QTdesigner,就可以引用PyQt5里的模块),把.ui的文件格式转换成.py的文件格式(有两种方式转换),这样就成功了。

首先在Pycharm里配置一下:

#####通过这个配置,可以引用PyQt5里的模块#####

“Name”:QTDesigner
“Group”:PyQt5
“Program”D:\Python\Lib\site-packages\PyQt5\designer.exe
“Working directory”:$ProjectFileDir$

Python&PyQt5 初次使用相关_第1张图片

#####这个的作用是将.ui的文件转换为.py的文件#####

“Name”: PyUIC
“Group”: PyQt5
“Program”: D:\Python\python.exe
“Arguments”:-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
“Working directory”: $FileDir$

Python&PyQt5 初次使用相关_第2张图片

#####由于在QT_designer的资源文件(比如图片,音频,视频等)将以.qrc的格式保存。该配置是将.qrc文件转换为.py的文件格式#####

“Name”: Rcc2Py
“Group”: PyQt5
“Program”: D:\Python\Lib\site-packages\PyQt5\pyrcc5.exe
“Arguments”:$FileName$ -o $FileNameWithoutExtension$_rc.py
“Working directory”: $FileDir$

Python&PyQt5 初次使用相关_第3张图片

 

2、如何将.ui文件或.qrc文件转换为.py的格式

第一种方法: 将命令行定位在ui文件所在文件夹(如何定位文件夹自行百度)下,输入pyuic5 filename.ui > py_name.py,即可转换,得到代码如下:

第二种方法:在Pycharm里选中.ui文件或.qrc文件(前提是QT_designer生成的.ui,.qrc文件已放在你的项目文件里了),单击鼠标右键,根据文件选择相应的转换,如下图:

Python&PyQt5 初次使用相关_第4张图片

3、在Pycharm里适当修改源码,显示GUI界面

下图为有.ui文件转化.py后的代码,

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'helin_canlender.ui'
#
# Created: Tue Aug 21 23:22:02 2018
#      by: PyQt5 UI code generator 5.4
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
   
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
        self.calendarWidget.setGeometry(QtCore.QRect(170, 120, 248, 169))
        self.calendarWidget.setObjectName("calendarWidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(270, 60, 71, 41))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        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", "MainWindow"))
        self.label.setText(_translate("MainWindow", "RILI"))





下图为修改后源码显示GUI(红色标注即为增加或修改的地方,可与前面对比)
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'helin_canlender.ui'
#
# Created: Tue Aug 21 23:22:02 2018
#      by: PyQt5 UI code generator 5.4
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui_MainWindow, self).__init__()
        self.setupUi(self)
        self.retranslateUi(self)
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
        self.calendarWidget.setGeometry(QtCore.QRect(170, 120, 248, 169))
        self.calendarWidget.setObjectName("calendarWidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(270, 60, 71, 41))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
        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", "MainWindow"))
        self.label.setText(_translate("MainWindow", "RILI"))

app = QtWidgets.QApplication(sys.argv)
win = Ui_MainWindow()
win.show()
sys.exit(app.exec_())

 

你可能感兴趣的:(python&PyQt5)