实验环境:
Python 2.7 version
Pycharm 2020
pyqt4 whl
注意pyqt4的whl可以在[python][whl]绝版资源pyqt4的whl格式文件python2.7-3.7系列_pyqt4 wheel-CSDN博客
下载whl后,执行
pip install PyQt4-4.11.4-cp27-cp27m-win_amd64.whl,whl里面已经有designer,pyuic和pyrcc我们无需在pyqt tools.
安装后我们在Pycharm配置QtDesign,打开Pycharm-File-Setting-External Tools-点击+号添加如下配置:
Name:qtdesigner
Group:Qt
Programs:D:\python\Lib\site-packages\PyQt4\designer.exe(这里是各位自己的designer路径,需要对应)
Working directory:$ProjectFileDir$
Pyuic的配置如下:
Name:pyuic
Group:Qt
Program:D:\python\python.exe(各位自己的python路径)
Arguments:-m PyQt4.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
Working directory:$FileDir$
注意:这样设置ui转换成py文件没有__name__==”__main__”如果你想自动生成可以这么搞:
Arguments:-m PyQt4.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py -x
Pyrcc配置:
配置参数如下:
Name:pyrcc
Group:Qt
Program:D:\python\pyrcc4.exe(各位自己的python路径)
Arguments:$FileName$ -o $FileNameWithoutExtension$_rc.py
Working directory:$FileDir$
至此就全部配置好了,回到PyCharm,Tools—>Qt—>QtDesigner,点击即可打开designer
示例:我随便在Qt界面拖放一个pushbutton按钮,然后就转成py文件,稍微改动就可以跑起来:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ui-a.ui'
#
# Created: Wed Jun 12 17:14:40 2019
# by: PyQt4 UI code generator 4.9.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_firstMainWindow(object):
def setupUi(self, firstMainWindow):
firstMainWindow.setObjectName(_fromUtf8("firstMainWindow"))
firstMainWindow.resize(556, 444)
firstMainWindow.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.centralwidget = QtGui.QWidget(firstMainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.queren = QtGui.QPushButton(self.centralwidget)
self.queren.setGeometry(QtCore.QRect(430, 360, 93, 28))
self.queren.setObjectName(_fromUtf8("queren"))
firstMainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(firstMainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 556, 23))
self.menubar.setObjectName(_fromUtf8("menubar"))
firstMainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(firstMainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
firstMainWindow.setStatusBar(self.statusbar)
self.retranslateUi(firstMainWindow)
QtCore.QMetaObject.connectSlotsByName(firstMainWindow)
def retranslateUi(self, firstMainWindow):
firstMainWindow.setWindowTitle(QtGui.QApplication.translate("firstMainWindow", "UI设计师", None, QtGui.QApplication.UnicodeUTF8))
self.queren.setText(QtGui.QApplication.translate("firstMainWindow", "确认", None, QtGui.QApplication.UnicodeUTF8))
if __name__=="__main__":
import sys
reload(sys) #
sys.setdefaultencoding( "utf-8" ) # 这两步是为了解决print中文报ascii编码错误
app=QtGui.QApplication(sys.argv)
widget= QtGui.QMainWindow()
ui= Ui_firstMainWindow()
ui.setupUi(widget)
widget.show()
sys.exit(app.exec_())