程序员的资源宝库

网站首页 > gitee 正文

PyQT5初学(一)

sanyeah 2024-04-19 23:51:57 gitee 5 ℃ 0 评论

PyQt5 是Digia的一套Qt5与python绑定的应用框架,同时支持2.x和3.x。本教程使用的是3.x。Qt库由Riverbank Computing开发,是最强大的GUI库之一 ,官方网站:www.riverbankcomputing.co.uk/news。

PyQt5是由一系列Python模块组成。超过620个类,6000和函数和方法。能在诸如Unix、Windows和Mac OS等主流操作系统上运行。PyQt5有两种证书,GPL和商业证书。

PyQt5类分为很多模块,主要模块有:

- QtCore 包含了核心的非GUI的功能。主要和时间、文件与文件夹、各种数据、流、URLs、mime类文件、进程与线程一起使用。
- QtGui 包含了窗口系统、事件处理、2D图像、基本绘画、字体和文字类。
- QtWidgets
- QtMultimedia
- QtBluetooth
- QtNetwork
- QtPositioning
- Enginio
- QtWebSockets
- QtWebKit
- QtWebKitWidgets
- QtXml
- QtSvg
- QtSql
- QtTest

QtWidgets类包含了一系列创建桌面应用的UI元素。
QtMultimedia包含了处理多媒体的内容和调用摄像头API的类。
QtBluetooth模块包含了查找和连接蓝牙的类。
QtNetwork包含了网络编程的类,这些工具能让TCP/IP和UDP开发变得更加方便和可靠。
QtPositioning包含了定位的类,可以使用卫星、WiFi甚至文本。
Engine包含了通过客户端进入和管理Qt Cloud的类。
QtWebSockets包含了WebSocket协议的类。
QtWebKit包含了一个基WebKit2的web浏览器。
QtWebKitWidgets包含了基于QtWidgets的WebKit1的类。
QtXml包含了处理xml的类,提供了SAX和DOM API的工具。
QtSvg提供了显示SVG内容的类,Scalable Vector Graphics (SVG)是一种是一种基于可扩展标记语言(XML),用于描述二维矢量图形的图形格式(这句话来自于维基百科)。
QtSql提供了处理数据库的工具。
QtTest提供了测试PyQt5应用的工具。

#######################################################################################################

 

目标:python用的多了,使用python生产exe文件,做一些工具来使用时最为方便的。那么,不用再去找c++了,直接用python

今天,用PyQT5开发出适合自己使用GUI界面,自己用的顺手的才是最好的。

文件1.Form基类,PyQT_Form.py

 1 # -*- coding: utf-8 -*-
 2 
 3 # Form implementation generated from reading ui file 'PyQT_Form.ui'
 4 #
 5 # Created by: PyQt5 UI code generator 5.11.3
 6 #
 7 # WARNING! All changes made in this file will be lost!
 8 
 9 from PyQt5 import QtCore, QtGui, QtWidgets
10 
11 class Ui_Form(object):
12     def setupUi(self, Form):
13         Form.setObjectName("Form")
14         Form.resize(753, 625)
15         self.pushButton = QtWidgets.QPushButton(Form)
16         self.pushButton.setGeometry(QtCore.QRect(30, 160, 91, 41))
17         self.pushButton.setObjectName("pushButton")
18         self.textEdit = QtWidgets.QTextEdit(Form)
19         self.textEdit.setGeometry(QtCore.QRect(30, 220, 701, 211))
20         font = QtGui.QFont()
21         font.setPointSize(20)
22         self.textEdit.setFont(font)
23         self.textEdit.setObjectName("textEdit")
24         self.pushButton_2 = QtWidgets.QPushButton(Form)
25         self.pushButton_2.setGeometry(QtCore.QRect(550, 160, 81, 41))
26         self.pushButton_2.setObjectName("pushButton_2")
27         self.pushButton_3 = QtWidgets.QPushButton(Form)
28         self.pushButton_3.setGeometry(QtCore.QRect(290, 40, 131, 51))
29         self.pushButton_3.setObjectName("pushButton_3")
30         self.toolButton = QtWidgets.QToolButton(Form)
31         self.toolButton.setGeometry(QtCore.QRect(30, 470, 91, 41))
32         self.toolButton.setObjectName("toolButton")
33         self.toolButton_2 = QtWidgets.QToolButton(Form)
34         self.toolButton_2.setGeometry(QtCore.QRect(550, 470, 81, 41))
35         self.toolButton_2.setObjectName("toolButton_2")
36         self.line = QtWidgets.QFrame(Form)
37         self.line.setGeometry(QtCore.QRect(10, 450, 741, 16))
38         self.line.setFrameShape(QtWidgets.QFrame.HLine)
39         self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
40         self.line.setObjectName("line")
41 
42         self.retranslateUi(Form)
43         self.pushButton.clicked.connect(Form.pushButton_click)
44         self.pushButton_2.released.connect(Form.pushButton_clear)
45         self.pushButton_3.clicked.connect(Form.pushButton_load_text)
46         self.toolButton.clicked.connect(Form.pushButton_up)
47         self.toolButton_2.clicked.connect(Form.pushButton_down)
48         QtCore.QMetaObject.connectSlotsByName(Form)
49 
50     def retranslateUi(self, Form):
51         _translate = QtCore.QCoreApplication.translate
52         Form.setWindowTitle(_translate("Form", "Form"))
53         self.pushButton.setText(_translate("Form", "显示"))
54         self.pushButton_2.setText(_translate("Form", "消失"))
55         self.pushButton_3.setText(_translate("Form", "读取文件"))
56         self.toolButton.setText(_translate("Form", "上一个"))
57         self.toolButton_2.setText(_translate("Form", "下一个"))

 

文件2.  自定义功能实现,每次修改ui都会更改基类py文件,必须单独建立实现模块,main.py

 1 """
 2     Function:GUI进入主函数
 3     Author: dengyexun
 4     DateTime:2018.11.17
 5 """
 6 import sys
 7 from PyQt5 import QtWidgets
 8 from PyQT_Form import Ui_Form
 9 from PyQt5.QtCore import *
10 from PyQt5.QtGui import *
11 from PyQt5.QtWidgets import *
12 
13 
14 # 自定义类
15 class MyPyQT_Form(QMainWindow, QWidget, Ui_Form):
16 
17     def __init__(self):
18         """
19             用父类初始化,建立ui
20         """
21         super(MyPyQT_Form, self).__init__()
22         self.setupUi(self)
23         self.setWindowTitle('App')
24         self.setWindowIcon(QIcon('./resources/car.png'))
25         self.first_word = ''
26         self.last_word = ''
27         self.count = 0
28 
29         QMainWindow.setFixedSize(self, 753, 625)
30 
31 
32     # 实现pushButton_click()函数,textEdit是文本框的id
33     def pushButton_click(self):
34         self.textEdit.setText('我爱你')
35 
36     # 实现pushButton_clear()函数,清空
37     def pushButton_clear(self):
38         self.textEdit.clear()
39 
40     # 实现pushButton_up()
41     def pushButton_up(self):
42         self.count -= 1
43         content = self.lines[self.count]
44         self.textEdit.setText(content)
45 
46     # 实现pushButton_down()
47     def pushButton_down(self):
48         self.count += 1
49         content = self.lines[self.count]
50         self.textEdit.setText(content)
51 
52     # 实现pushButton_load_text(),读取文件内容
53     def pushButton_load_text(self):
54         print('load text...')
55         # 初始化这个实例,设置一些基本属性
56         dlg = QFileDialog()
57         dlg.setFileMode(QFileDialog.AnyFile)
58         dlg.setFilter(QDir.Files)
59         # 当选择关闭的时候
60         if dlg.exec_():
61             # 拿到当前读取的文本
62             filenames = dlg.selectedFiles()
63             # 读取文本内容,设置到textEdit中来
64             with open(filenames[0], 'r', encoding='utf8') as fr:
65                 self.lines = fr.readlines()
66                 self.textEdit.setText(self.lines[0])
67 
68 
69 if __name__ == '__main__':
70     # 建立应用
71     app = QtWidgets.QApplication(sys.argv)
72     # 实例化
73     my_pyqt_form = MyPyQT_Form()
74     # 显示
75     my_pyqt_form.show()
76     # 直接退出程序的首选方法
77     sys.exit(app.exec_())

 

文件3  打包成exe,使用pyinstaller 打包main.py文件,gen_exe.py

 1 """
 2     Function:打包py文件成exe
 3     Author: dengyexun
 4     DateTime:2018.11.19
 5 """
 6 import os
 7 
 8 
 9 # 打包,打包好的exe在Scripts的dist文件夹下
10 def package_exe():
11     current_py = os.path.join(os.getcwd(), 'main.py')
12     print(current_py)
13     des_dir = os.chdir('D:\Python35\Scripts')
14     print(os.getcwd())
15     do_exe = 'pyinstaller.exe -F -w ' + current_py
16     r_v = os.system(do_exe)
17 
18     print('ok')
19 
20 
21 if __name__ == '__main__':
22     package_exe()
23     os.system("pause")

 

GUI界面

 

猜你喜欢

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表