环境如下:
Mac OS 10.13.6 版本
Pycharm 10.13.6 社区版
python 3.6.6 版本
PyQt5 5.13.0版
- 进入虚拟环境并激活(不需要在虚拟环境的跳过第一步)
$ cd /Users/***/PycharmProjects/pyqt/env
$ source bin/activate
- 进入Pycharm搜索安装SIP、PyQt5或者命令行输入
pip3 install SIP
接着输入pip3 install PyQt5
- 安装Qt
brew install qt
-
在Pycharm配置QtDesigner,可以可视化操作的工具。
Program:找到自己电脑里的Designer.app对应的路径
打开终端输入:
find ~ -iname "Designer.app"
查看Designer.app的显示简介并复制路径
eg: /usr/local/Cellar/qt/5.13.0/libexec/Designer.app
Working directory:点击insert Macro..添加$FileDir$
-
添加pyUIC能把UI文件转成py文件
Program:找到自己工程使用的python对应的路径
eg: /Users/***/PycharmProjects/pyqt/env/bin/python
Paramters里面的内容:(一样)
-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
Working directory:添加$FileDir$
点击Ok,apply
打开QtDesigner。
pyUIC这个命令行工具会把UI文件转成py文件
如果上面觉得麻烦的话
可以保存下面这个脚本,也能将.ui文件转换为.py文件,同一个文件夹下的所有.ui文件一次转换成.py文件
import os
import os.path
dir = './'
def listUiFile():
list = []
files = os.listdir(dir)
for filename in files:
# print( dir + os.sep + f )
# print(filename)
if os.path.splitext(filename)[1] == '.ui':
list.append(filename)
return list
def transPyFile(filename):
return os.path.splitext(filename)[0] + '.py'
def runMain():
list = listUiFile()
for uifile in list:
pyfile = transPyFile(uifile)
cmd = 'pyuic5 -o {pyfile} {uifile}'.format(pyfile=pyfile, uifile=uifile)
# print(cmd)
os.system(cmd)
if __name__ == "__main__":
runMain()
- Qt第一个界面,运行试一下
新建demo1.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(708, 600)
MainWindow.setStyleSheet("background:rgb(20, 142, 198)")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame1_3 = QtWidgets.QFrame(self.centralwidget)
self.frame1_3.setGeometry(QtCore.QRect(60, 140, 531, 271))
self.frame1_3.setStyleSheet("background:rgb(53,144,250);")
self.frame1_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame1_3.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame1_3.setObjectName("frame1_3")
self.frame1_2 = QtWidgets.QFrame(self.frame1_3)
self.frame1_2.setGeometry(QtCore.QRect(130, 130, 293, 49))
self.frame1_2.setStyleSheet(" border-style:none;")
self.frame1_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame1_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame1_2.setObjectName("frame1_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.frame1_2)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_3 = QtWidgets.QLabel(self.frame1_2)
self.label_3.setStyleSheet("color:rgb(255, 255, 255);")
self.label_3.setObjectName("label_3")
self.horizontalLayout_2.addWidget(self.label_3)
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame1_2)
self.lineEdit_2.setStyleSheet("background:rgb(255, 255, 255);height:30;")
self.lineEdit_2.setObjectName("lineEdit_2")
self.horizontalLayout_2.addWidget(self.lineEdit_2)
self.frame1 = QtWidgets.QFrame(self.frame1_3)
self.frame1.setGeometry(QtCore.QRect(130, 80, 293, 49))
self.frame1.setStyleSheet(" border-style:none;")
self.frame1.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame1.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame1.setObjectName("frame1")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.frame1)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_2 = QtWidgets.QLabel(self.frame1)
self.label_2.setStyleSheet("color:rgb(255, 255, 255);")
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
self.lineEdit = QtWidgets.QLineEdit(self.frame1)
self.lineEdit.setStyleSheet("background:rgb(255, 255, 255);height:30;")
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout.addWidget(self.lineEdit)
self.click = QtWidgets.QPushButton(self.frame1_3)
self.click.setGeometry(QtCore.QRect(230, 200, 113, 32))
self.click.setStyleSheet("color:rgb(255, 255, 255);background:rgb(128, 255, 7);")
self.click.setObjectName("click")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 708, 22))
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.click.clicked.connect(MainWindow.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_3.setText(_translate("MainWindow", "密码 "))
self.label_2.setText(_translate("MainWindow", "用户名"))
self.click.setText(_translate("MainWindow", "登录"))
新建main.py
import sys
import demo1
from PyQt5.QtWidgets import QApplication, QMainWindow
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = demo1.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
运行main.py看看成功安装成功没有