diff --git a/by_PyQt5/window-new.py b/by_PyQt5/window-new.py new file mode 100644 index 0000000..26e1bf1 --- /dev/null +++ b/by_PyQt5/window-new.py @@ -0,0 +1,104 @@ +# -*- coding: utf-8 -*- +""" +电脑二维码识别软件,对当前屏幕截屏,识别截屏里面的二维码,并将识别到的信息列出来 +可以双击具体链接打开,也可用点击打开全部一次性打开全部 +""" +import sys +from PIL import ImageGrab +from pyzbar import pyzbar +import webbrowser +import time + +# Form implementation generated from reading ui file 'window.ui' +# +# Created by: PyQt5 UI code generator 5.15.9 +# +# WARNING: Any manual changes made to this file will be lost when pyuic5 is +# run again. Do not edit this file unless you know what you are doing. + + +from PyQt5 import QtCore, QtGui, QtWidgets + + +class Ui_MainWindow(object): + def __init__(self, mainwindow): + self.mainwindow = mainwindow + self.setupUi(mainwindow) + + def setupUi(self, MainWindow: QtWidgets.QMainWindow): + MainWindow.setObjectName("MainWindow") + MainWindow.setFixedSize(396, 233) # 固定窗口大小,本来是resize,修改为这个禁止人为拉动软件size + MainWindow.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) # 窗口始终置顶 + self.centralwidget = QtWidgets.QWidget(MainWindow) + self.centralwidget.setObjectName("centralwidget") + self.listWidget = QtWidgets.QListWidget(self.centralwidget) + self.listWidget.setGeometry(QtCore.QRect(30, 90, 321, 91)) + self.listWidget.setObjectName("listWidget") + self.pushButton = QtWidgets.QPushButton(self.centralwidget) + self.pushButton.setGeometry(QtCore.QRect(50, 20, 121, 51)) + self.pushButton.setObjectName("pushButton") + self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) + self.pushButton_2.setGeometry(QtCore.QRect(200, 30, 111, 41)) + self.pushButton_2.setObjectName("pushButton_2") + MainWindow.setCentralWidget(self.centralwidget) + self.statusbar = QtWidgets.QStatusBar(MainWindow) + self.statusbar.setObjectName("statusbar") + MainWindow.setStatusBar(self.statusbar) + + self.retranslateUi(MainWindow) + QtCore.QMetaObject.connectSlotsByName(MainWindow) + + # 将相应事件与相应回调函数绑定 + self.pushButton.clicked.connect(self.grab) + self.pushButton_2.clicked.connect(self.open_url) + self.listWidget.itemDoubleClicked.connect(self.double_open) + + def retranslateUi(self, MainWindow): + _translate = QtCore.QCoreApplication.translate + MainWindow.setWindowTitle(_translate("MainWindow", "二维码截屏识别")) + self.pushButton.setText(_translate("MainWindow", "截屏识别")) + self.pushButton_2.setText(_translate("MainWindow", "打开全部")) + + # 以下为自定义函数,用来实现自定义功能,将与各个组件的相应事件绑定 + def grab(self): + # self.mainwindow.showMinimized() + self.mainwindow.hide() + time.sleep(0.3) + img = ImageGrab.grab() # 截屏 + # self.mainwindow.showNormal() + self.mainwindow.show() + # img.save('demo.png') + results = pyzbar.decode(image=img, symbols=[pyzbar.ZBarSymbol.QRCODE]) # 识别二维码 + self.listWidget.clear() # 先清空以前的信息 + for i, result in enumerate(results): + url = result.data.decode('utf-8') + # print(url) + self.listWidget.insertItem(i, url) # 这里的url是str,不是QListWidgetItem,主打方便 + + def open_url(self): + for i in range(self.listWidget.count()): # count计数有多少个item,便于按row获取item + webbrowser.open(url=self.listWidget.item(i).text()) # item(row)获取元素,text()返回str + + def double_open(self): + url = self.listWidget.selectedItems()[0].text() # 选中的item可以是多个,但是double的时候只会有一个所以[0] + webbrowser.open(url=url) + + +if __name__ == '__main__': + # # 必须的app和sys.exit(和tk.mainloop一样) + # app = QtWidgets.QApplication(sys.argv) + # MainWindow = QtWidgets.QMainWindow() + # # 将创建的ui实例化,应用起来 + # ui = Ui_MainWindow() + # # ui只是框架,需要传入一个QMainWindow对象作为实体来应用该ui + # ui.setupUi(MainWindow) + # # 显示ui(窗口) + # MainWindow.show() + # + # sys.exit(app.exec_()) + + app = QtWidgets.QApplication(sys.argv) + win = QtWidgets.QMainWindow() + ui = Ui_MainWindow(win) + win.show() + sys.exit(app.exec_())