Qt hello

PyQt 入门

Python体系下GUI框架也多了去了,PyQt算是比较受欢迎的一个。如果对Qt框架熟悉,那掌握这套框架是很简单的。

一,安装

1.PyQt5

pip3 install PyQt5

2.Designer UI工具

注:这个工具对python版本有要求,貌似只支持到3.9 所以如果python高于这个版本,可以单独安装 QtDesigner工具 (pip3 install PyQt5Designer)

pip3 install PyQt5-tools

3.UI文件转py文件工具。

python下UI文件无法直接使用,需要使用这个工具转成py文件。

注:windows 下我的python版本为3.12 ,自带了这个工具,无需单独安装。

sudo apt-get install pyqt5-dev-tools

二,使用

1.创建UI文件。

使用designer工具创建ui文件。随便放几个控件,然后保存到工程目录下,取名为widget.ui。

我的designer工具位于这里:

 /home/keiler/.local/lib/python3.11/site-packages/qt5_applications/Qt/bin/designer 

2.UI文件转py文件。

pyuic5 -o widget.py   widget.ui

3,主程序加载py文件。

import sys
from PyQt5.QtWidgets import QApplication, QWidget

from widget import Ui_Form

#Ui_Form 为 Ui文件中的类
class MyApp(Ui_Form, QWidget):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        #信号槽的连接
        self.pushButton.clicked.connect(lambda:print("hello"))
        self.pushButton_2.clicked.connect(self.fun)
        self.show()

    def fun(self):
        self.textEdit.append("good")
 
# 应用程序入口
if __name__ == "__main__":
    app = QApplication(sys.argv)
    my_app = MyApp()
    sys.exit(app.exec_())

4,效果

1 评论

  1. Good Day

    I have just checked qthello.com for the ranking keywords and saw that your website could use an upgrade.

    We will enhance your ranks organically and safely, using only state of the art AI and whitehat methods, while providing monthly reports and outstanding support.

    More info:
    https://www.digital-x-press.com/unbeatable-seo/

    Regards
    Mike Hawkins

    Digital X SEO Experts

发表回复