Skip to content

Commit

Permalink
rewrite GUI with PyQt5
Browse files Browse the repository at this point in the history
and you can double click specific url to open it
  • Loading branch information
faf4r authored Jul 7, 2023
1 parent 2e37068 commit f4429f6
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 0 deletions.
87 changes: 87 additions & 0 deletions by_PyQt5/window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# -*- coding: utf-8 -*-
"""
电脑二维码识别软件,对当前屏幕截屏,识别截屏里面的二维码,并将识别到的信息列出来
可以双击具体链接打开,也可用点击打开全部一次性打开全部
"""
import sys
from PIL import ImageGrab
from pyzbar import pyzbar
import webbrowser

# 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 setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setFixedSize(396, 233) # 固定窗口大小
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):
img = ImageGrab.grab()
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)

def open_url(self):
for i in range(self.listWidget.count()):
webbrowser.open(url=self.listWidget.item(i).text())

def double_open(self):
url = self.listWidget.selectedItems()[0].text()
webbrowser.open(url=url)


if __name__ == '__main__':
# 必须的app和sys.exit(和tk.mainloop一样)
app = QtWidgets.QApplication(sys.argv)

# 将创建的ui实例化,应用起来
ui = Ui_MainWindow()
MainWindow = QtWidgets.QMainWindow()
# ui只是框架,需要传入一个QMainWindow对象作为实体来应用该ui
ui.setupUi(MainWindow)
# 显示ui(窗口)
MainWindow.show()

sys.exit(app.exec_())
58 changes: 58 additions & 0 deletions by_PyQt5/window.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>396</width>
<height>233</height>
</rect>
</property>
<property name="windowTitle">
<string>二维码截屏识别</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QListWidget" name="listWidget">
<property name="geometry">
<rect>
<x>30</x>
<y>90</y>
<width>321</width>
<height>91</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>50</x>
<y>20</y>
<width>121</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>截屏识别</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>200</x>
<y>30</y>
<width>111</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>打开全部</string>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
41 changes: 41 additions & 0 deletions by_PyQt5/window_pyuic_convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-

# 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 setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(396, 233)
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)

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "二维码截屏识别"))
self.pushButton.setText(_translate("MainWindow", "截屏识别"))
self.pushButton_2.setText(_translate("MainWindow", "打开全部"))

0 comments on commit f4429f6

Please sign in to comment.