PyQt
用 Qt Designer 设计界面 ,生成 .ui
格式 的 界面 用 pyuic5
将 .ui
转为.py
- 构建
主 程 序 打 包 .py
为可执行文 件
pip3 install PyQt5
pip3 install pyqt5-tools
pip3 install pyuic5-tool
bin
.ui
。
Qt Designer
选择 QVGA 纵向:
现在编辑
QLabel
:标签,该标签可以放纯文本 ,链接或 者 富 文 本 信 息 。
QTextEdit
:多 行文 本 框 ,可 以显示 多 行文 本 内容 ,当 文 本内 容 超 出 控 件 显示范围时,可 显示滚动条 ,Qtextedit
不 仅可以用来 显示文 本 还可以用来 显示 HTML文 档。
QPushButton
:命令 按钮,按下(或 者 单击)按钮以命令 脚本 执行某 个操作 。
QLabel
QPushButton
QLabel
QLabel
请输
,
alignment
Download
:
scihub_ui.ui
.ui
转换为 python pyuic5
pyuic5 -o scihub_ui.py scihub_ui.ui
转换scihub_ui.py
import
Ui_MainWindow
class
,这个 class
setupUi()
retranslateUi()
。setupUi()
self.xx
,这与Qt Designer
retranslateUi()
设置
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'scihub_ui.ui'
#
# Created by: PyQt5 UI code generator 5.12.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(240, 320)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(10, 10, 211, 251))
self.widget.setObjectName("widget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(self.widget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.textEdit = QtWidgets.QTextEdit(self.widget)
self.textEdit.setObjectName("textEdit")
self.verticalLayout.addWidget(self.textEdit)
self.pushButton = QtWidgets.QPushButton(self.widget)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 240, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "请输入 DOI"))
self.pushButton.setText(_translate("MainWindow", "Download"))
这里scihub_gui.py
## 单继承方法 ,能 更 好地 进行界面 与 逻辑的 分 离
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.Qt import QStandardPaths
from scihub_ui import Ui_MainWindow
class CustomUI(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent) # 调用父 类构造 函数 ,self 就是一 个 QMainWindow 对象
self.ui = Ui_MainWindow() # 创建UI 对象
self.ui.setupUi(self) # 构造UI
if __name__ == '__main__':
app = QApplication(sys.argv) # 创建app,用 QApplication 类
cutomUI = CustomUI()
cutomUI.show()
sys.exit(app.exec_())
这个DOI | PMID | URL
from scihub import SciHub
sh = SciHub()
result = sh.download('http://ieeexplore.ieee.org/xpl/login.jsp?tp=&arnumber=1648853', path='paper.pdf')
Scihub.py
scihub_gui.py
PyPDF2
from PyPDF2 import PdfFileReader
with open('paper.pdf', 'rb') as f:
pdf = PdfFileReader(f)
info = pdf.getDocumentInfo()
title = info.title
PyQt5
- 识别黏贴
板 的 变化 - 识别按钮动作
scihub_gui.py
QApplication.clipboard()
读取黏贴QMessageBox
弹出scihub_gui.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
from PyQt5.Qt import QStandardPaths
from scihub_ui import Ui_MainWindow
from scihub import SciHub
from PyPDF2 import PdfFileReader
import os
class CustomUI(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.cb = QApplication.clipboard()
self.cb.dataChanged.connect(self.monitor_clipboard)
self.ui.textEdit.setText(self.cb.text())
self.ui.pushButton.clicked.connect(self.buttClicked)
def monitor_clipboard(self):
if self.cb.text():
self.ui.textEdit.setText(self.cb.text())
def buttClicked(self):
try:
curPath = QStandardPaths.writableLocation(QStandardPaths.DocumentsLocation) # 默 认保存在 ~/Documents
pdf_name = "/paper.pdf" # 默 认文件名 为 paper.pdf
test = SciHub()
textEditber = self.ui.textEdit.toPlainText()
test.download(textEditber, path= curPath + pdf_name)
with open(curPath + pdf_name, 'rb') as f:
pdf = PdfFileReader(f)
info = pdf.getDocumentInfo()
title = info.title
if title != None and title != "":
file_name = title.replace(' ',"_")
file_name = file_name + ".pdf"
os.rename(curPath + pdf_name, curPath + "/" + file_name)
else:
file_name = "paper.pdf"
except:
QMessageBox.information(self, '', '下 载失败')
else:
QMessageBox.information(self, '', file_name + '下 载完成 ')
if __name__ == '__main__':
app = QApplication(sys.argv)
cutomUI = CustomUI()
cutomUI.show()
sys.exit(app.exec_())
对于
一 些功能 较复杂的程 序 我 们可以把这些进行逻辑操作 的 函数 放 到 一 个新的 .py
文 件 中 ,这样可 以方便 后 期 维护代 码。但 在 这里因 为程序 比 较简单,所 以就直接 放 在 主 程 序 中 了 。踩过
的 坑 :在 Qt中 ,当 我 们需要 不 调用 QFileDialog保存 文 件 时,得用 QStandardPaths
指定 路 径 ,否 则打包 出来 的 软件没 法 保存 。
这里pyinstaller
pyinstaller
:
pip install pyinstaller
可用 网站转换 png至 ico格式 :https://www.easyicon.net/covert/
pyinstaller --windowed --onefile --clean --noconfirm scihub_gui.py -p scihub.py -i scihub.icns
dist
创建环境:
conda create -n scihubgui python=3.7
conda activate scihubgui
pip install -r requirements.txt
requirements.txt
:
altgraph==0.17
beautifulsoup4==4.9.3
certifi==2020.6.20
chardet==3.0.4
idna==2.10
macholib==1.14
powerline-status==2.7
pyinstaller==4.0
pyinstaller-hooks-contrib==2020.9
PyPDF2==1.26.0
PyQt5==5.15.1
PyQt5-sip==12.8.1
PySocks==1.7.1
requests==2.24.0
retrying==1.3.3
six==1.15.0
soupsieve==2.0.1
urllib3==1.25.11
pyinstaller --windowed --onefile --clean --noconfirm scihub_gui.py -p scihub.py -i scihub.icns
现在
- Qt Documentation: https://doc.qt.io/
- Qt for Python: https://doc.qt.io/qtforpython/#project
- PyQt5
中 文教 程 : https://github.com/maicss/PyQt5-Chinese-tutorial - 《Python Qt GUI
与 数 据 可 视化编程》