PY QT

pyqt 的安装使用

pyqt python

Posted by gomyck on October 30, 2023

pyqt 的安装使用

在 macos 上安装 pyqt 非常的不方便, 首先要用 brew 安装 QT, 然后在使用 conda 安装 pyqt, 在 idea 安装 ext tools 也非常的不方便

推荐在 windows 上安装 pyqt

使用 conda install pyqt 即可

conda 安装好之后, 下载美化版的pyqt, https://github.com/zhiyiYo/PyQt-Fluent-Widgets/releases 开箱即用

使用:

  • 在控制台输入: designer.exe 即可打开设计器 或 直接双击 fluent 的可执行程序开箱即用
  • pyuic5.bat .\untitled.ui -o .\untitled.py

技巧

  • 刚进入设计页面的时候, 要选择窗体, 一般主界面都是 Main window, 而弹窗是 dialog
  • 右键点击面板, 改变信号槽, 可以添加自定义槽名(相当于自定义方法)
  • 点击工具栏的 编辑 信号/槽 可以进入拖拽模式, 来选择按钮的槽

参考资料: https://blog.csdn.net/Strive_For_Future/article/details/126748989 参考资料: https://lovesoo.org/2020/03/14/pyqt-getting-started/ 参考资料: https://mathpretty.com/13624.html 参考资料: https://www.cnblogs.com/linyfeng/p/11223707.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog
from PyQt5 import uic
import sys
# 默认的类名是 Ui_MainWindow
from untitled import Ui_MainWindow

# 如果想使用代码调用的话, 继承 Ui_MainWindow
form_class = uic.loadUiType("untitled.ui")[0]  # Load the UI
class MyWindowClass(QMainWindow, form_class):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.setupUi(self)
    # 在 designer 里定义了槽函数 open_file_dialog
    def open_file_dialog(self):
        options = QFileDialog.Options()
        fileName, _ = QFileDialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*)", options=options)
        if fileName:
            print(fileName)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWindow = MyWindowClass(None)
    myWindow.show()
    app.exec_()