pyqt环境安装
PyQt(一)
环境: pycharm、python3.6
插件安装
QtDesigner-界面设计
PyUIC-ui转py
Pyqcc-资源转py
简单例子
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QAction, qApp, QWidget, QDesktopWidget
from PyQt5.QtGui import QIcon
from ui.ui_main import Ui_MainWindow
class Example(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.initUI()
def initUI(self):
self.statusBar().showMessage("Ready")
exitAction = QAction(QIcon("Res/img/icon1.png"), "Exit1", self)
exitAction.setShortcut("Ctrl+Q")
exitAction.setStatusTip("exit application")
exitAction.triggered.connect(qApp.quit)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu("&File")
fileMenu.addAction(exitAction)
self.toolBar = self.addToolBar("Exit")
self.toolBar.addAction(exitAction)
self.Center()
def Center(self):
# 居中
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
效果
图片资源的拆入
创建资源文件
创建前缀并添加文件
使用Pyqcc转换qrc文件得到py文件
安装pyqt
进入cmd界面。执行命令pip3 install --index-url https://pypi.douban.com/simple PyQt5 ,等待一会儿,命令执行完毕后PyQt5就安装好了
安装缺少的PyQt5 tools,可使用以下源:
pip install PyQt5-tools -i http://pypi.douban.com/simple --trusted-host=pypi.douban.com
测试
import sys
from PyQt5 import QtWidgets, QtCore
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
widget.resize(400, 100)
widget.setWindowTitle("This is a demo for PyQt Widget.")
widget.show()
exit(app.exec_())
pyqt5异常奔溃没有日志
有时pyqt程序奔溃没有日志,需要打开pycharm设置
安装第三方库
默认地址是https://pypi.python.org/simple国外地址下载会非常慢,这里可以选择 Manage Repositories添加国内pip镜像
这里推荐三个非常不错的国内镜像
https://pypi.tuna.tsinghua.edu.cn/simple/ 清华大学镜像
http://pypi.douban.com/simple/ 豆瓣镜像
http://mirrors.aliyun.com/pypi/simple/ 阿里镜像
在不行,从 码云 上下载手动安装