(编辑:jimmy 日期: 2024/11/14 浏览:2)
一、安装
首先根据自己的python版本下载pyqt4离线包,现在连接:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyqt4
比如我地python版本是python3.5.2,我选择PyQt4"text-align: center">
在whl文件路径下,使用pip指令安装
pip install PyQt4"color: #ff0000">二、pycharm下环境配置
找到:File——>Settings——>Tools——>External Tools
添加QtDesigner、PyUIC、PyRcc三个工具,如下图所示:
1. QtDesigner,program填“自己的python路径\python\Lib\site-packages\PyQt4\designer.exe”,Arguments不用填,Working Directory填:$ProjectFileDir$。
2.PyUIC,program填“自己的python路径\python\python.exe”,Arguments填“-m PyQt4.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py”,Working Directory填:$ProjectFileDir$。
3. PyRcc,program填“自己的python路径\python\Scripts\pyrcc5.exe”,Arguments填“$FileName$ -o $FileNameWithoutExtension$_rc.py”,Working Directory填:$ProjectFileDir$。
三、简单使用
使用QtDesigner设计了一个简单的界面,保存为test.ui。
然后在pycharm中右击test.ui 文件,使用pyUIC生成test.py文件。
生成的test.py文件如下:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'test.ui' # # Created by: PyQt4 UI code generator 4.11.4 # # WARNING! All changes made in this file will be lost! from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_Form(object): def setupUi(self, Form): Form.setObjectName(_fromUtf8("Form")) Form.resize(587, 457) self.TestBtn = QtGui.QPushButton(Form) self.TestBtn.setGeometry(QtCore.QRect(0, 10, 75, 51)) self.TestBtn.setObjectName(_fromUtf8("TestBtn")) self.ShowPte = QtGui.QPlainTextEdit(Form) self.ShowPte.setGeometry(QtCore.QRect(80, 10, 491, 431)) self.ShowPte.setObjectName(_fromUtf8("ShowPte")) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): Form.setWindowTitle(_translate("Form", "Form", None)) self.TestBtn.setText(_translate("Form", "按钮", None)) self.ShowPte.setPlainText(_translate("Form", "窗前明月光,\n" "疑是地上霜。\n" "举头望明月,\n" "低头思故乡。", None))然后创建一个main.py来启动程序。
# -*- coding: utf-8 -*- import sys from PyQt4 import QtCore, QtGui from test import * class MyWindows(QtGui.QWidget): def __init__(self): super(MyWindows,self).__init__() Ui_Form().setupUi(self) if __name__ == '__main__': app = QtGui.QApplication(sys.argv) ui=MyWindows() ui.show() app.exec()运行main.py文件,得到如下界面。