1. install brew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2. install python3
brew install python3
brew info python3
python3 -V
3. install sip & pyqt5
pip3 install sip pip3 install pyqt5
simple.py
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
import sys from PyQt5.QtWidgets import QMainWindow, QTextEdit, QApplication, qApp, QAction from PyQt5.QtGui import QIcon class simple(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setWindowTitle('hello') self.setObjectName("helloWin") self.resize(500, 300) if __name__ == '__main__': app = QApplication(sys.argv) ex = simple() ex.show() sys.exit(app.exec_())
simple2.py
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
# -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * class FirstWindow(QWidget): signal = pyqtSignal(str) def __init__(self, parent=None): # super这个用法是调用父类的构造函数 # parent=None表示默认没有父Widget,如果指定父亲Widget,则调用之 super(FirstWindow, self).__init__(parent) self.resize(100, 100) self.btn = QToolButton(self) self.btn.setText("click") self.signal.connect(self.sinCall) def sinCall(self, text): print("sin emit:", text) self.btn.setText(text) def closeEvent(self, event): result = QMessageBox.question(self, "Confirm Exit...", "Are you sure you want to exit ?", QMessageBox.Yes| QMessageBox.No) event.ignore() if result == QMessageBox.Yes: event.accept() class SecondWindow(QWidget): def __init__(self, parent=None): super(SecondWindow, self).__init__(parent) self.resize(200, 200) self.setStyleSheet("background: black") def handle_click(self): if not self.isVisible(): self.show() def handle_close(self): self.close() if __name__ == "__main__": App = QApplication(sys.argv) ex = FirstWindow() s = SecondWindow() ex.btn.clicked.connect(s.handle_click) #ex.btn.clicked.connect(ex.hide) ex.signal.emit("signal text") ex.show() sys.exit(App.exec_())
4. install Qt (可选, 方便做界面,类似winform做界面用: 界面可视化工具,可以很方便的设计UI,所见即所得)
a. install xcode
b. download Qt https://www.qt.io/download/
5. install pycharm(可选, IDE, 方便开发: 可集成Qt; Qt的ui文件转py; 直接运行python)
a. 配置Qt Designer: Qt的界面可视化工具,可以很方便的设计UI,所见即所得
- 打开 PyCharm 的设置
- 找到 Tools -> External Tools
- 点击 + -> Create Tool
b. 配置PyUIC: 这个工具可以把Qt Designer的UI文件,转换成Py文件
python3 -m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py
c. 右键菜单中选择 External Tools 即可
d. 配置 pyrcc (安装完python3以后就有了)pyrcc5 resources.qrc -o resources.py
使用resource.qrc <!DOCTYPE RCC> <RCC version="1.0"> <qresource prefix="/img"> <file alias="a.png">./img/a.png</file> <file alias="b.png">./img/b.png</file> </qresource> </RCC> pyrcc5 img_rcc.qrc -o img.py # 导入这个文件,会自动调用资源初始化函数 from img.py import * # 使用资源的时候 访问的文件名 即 qrc里的 :prefix+alias ,例如 img = QPixmap(":/img/a.png")
6.install pyinstaller (可选, python转可执行文件:exe, pkg))
pip3 install pyinstaller pyinstaller -F -w {PyFile}.py