基于PYQT5的截图翻译工具
功能介绍
- 翻译功能
- 截图功能(快捷键 + 截图存储到剪切板中)
- 文字识别OCR(基于百度API的文字识别)
UI 界面
截图
截图可以使用第三方截图 或 使用PyQt5截图
此文章使用PyQt5的截图
class Snipper(QtWidgets.QWidget):
def __init__(self, parent=None, flags=Qt.WindowFlags()):
super().__init__(parent=parent, flags=flags)
self.setWindowTitle("TextShot")
self.setWindowFlags( # 设置窗口属性,窗口置顶
Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Dialog
)
self.setWindowState(self.windowState() | Qt.WindowFullScreen)
self.screen = QtGui.QScreen.grabWindow(
QtWidgets.QApplication.primaryScreen(), # 获取当前屏幕分辨率(屏幕大小)
QtWidgets.QApplication.desktop().winId(),
)
palette = QtGui.QPalette() # 调色板QPalette类
palette.setBrush(self.backgroundRole(), QtGui.QBrush(self.screen))
self.setPalette(palette)
# 改变鼠标形状
QtWidgets.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
self.start, self.end = QtCore.QPoint(), QtCore.QPoint()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
QtWidgets.QApplication.quit()
return super().keyPressEvent(event)
def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setPen(Qt.NoPen)
painter.setBrush(QtGui.QColor(0, 0, 0, 100))
painter.drawRect(0, 0, self.width(), self.height())
if self.start == self.end:
return super().paintEvent(event)
painter.setPen(QtGui.QPen(QtGui.QColor(255, 255, 255), 3))
painter.setBrush(painter.background())
painter.drawRect(QtCore.QRect(self.start, self.end))
return super().paintEvent(event)
def mousePressEvent(self, event):
self.start = self.end = QtGui.QCursor.pos()
self.update()
return super().mousePressEvent(event)
def mouseMoveEvent(self, event):
self.end = QtGui.QCursor.pos()
self.update()
return super().mousePressEvent(event)
def mouseReleaseEvent(self, event):
if self.start == self.end:
return super().mouseReleaseEvent(event)
self.hide()
QtWidgets.QApplication.processEvents()
if self.start.x() < self.end.x():
shot = self.screen.copy(QtCore.QRect(self.start, self.end))
else:
shot = self.screen.copy(QtCore.QRect(self.end, self.start))
QtWidgets.QApplication.quit()
翻译
翻译功能可以请求百度翻译或者有道翻译,网上有很多这里就不多赘述。
文字识别
文字识别本文字使用的百度云的文字识别OCR 百度云文字识别
全局热键(快捷键)
开启多线程捕获全局全局热键,防止主线程阻塞导致假死。
安装
pip install system_hotkey
示例
from system_hotkey import SystemHotkey
from PyQt5.QtCore import QObject, pyqtSignal, Qt
class HotKeyThread(QObject):
trigger = pyqtSignal(str)
def __init__(self):
super().__init__()
# 1. 自定义热键响应
self.trigger.connect(self.hotKeyEvent)
# 2.初始化热键
self.hot_key = SystemHotkey()
# 3.绑定快捷键发送信号
self.hot_key.register(('control','1'),, callback=lambda x: self.sendKeyEvent("按下"))
def sendKeyEvent(self,data):
self.trigger.emit(data)
def hotKeyEvent(self,data):
print("接收信号:{}".format(data))
托盘管理
托盘管理使用pyqt5的 QSystemTrayIcon
实现
class Tray(QSystemTrayIcon):
def __init__(self, UI):
super(Tray, self).__init__()
self.ui = UI
self.setIcon(QIcon('icons/1.ico')) # 托盘图标
self.setToolTip('小工具') # 鼠标移动到托盘图标上的提示
self.activated.connect(self.clickedIcon) # 点击信号
self.menu()
self.show()
def clickedIcon(self,reason):
# reason:鼠标点击托盘图标时传递的整数型信号
# 1表示单击右键
# 2表示双击左键
# 3表示单击左键
# 4表示点击中键
# 下面定义单击左键是弹出或隐藏界面,单击右键是弹出菜单。
if reason == 3:
self.trayClickedEvent()
elif reason == 1:
self.contextMenu()
def menu(self):
menu = QMenu()
action = QAction('退出', self, triggered=self.triggered)
menu.addAction(action)
self.setContextMenu(menu)
def trayClickedEvent(self):
if self.ui.isHidden():
self.ui.setHidden(False)
if self.ui.windowState() == Qt.WindowMinimized:
self.ui.showNormal() # 正常显示窗口
self.ui.raise_() # 控制在上层
self.ui.activateWindow() # 活动窗口
else:
self.ui.setHidden(True) # 设置隐藏窗口
def triggered(self):
self.deleteLater() # 删除托盘图标,无此操作的话,程序退出后托盘图标不会自动清除
qApp.quit() # 会重写closeEvent,换一个退出程序的命令
主代码
import sys
import time
from tray import Tray
from ui import Ui_MainWindow
from demo import translate,Child_Dialog
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QThread,pyqtSignal,Qt
from PyQt5.QtWidgets import QApplication,QSystemTrayIcon,QMenu,QAction,qApp
class HotKeyThread(QThread,SystemHotkey):
# 开启多线程全局热键
trigger = pyqtSignal()
def __init__(self,UI):
self.ui = UI
super(HotKeyThread,self).__init__()
self.register(('control', '1'), callback=lambda x: self.start())
self.trigger.connect(self.hotKeyEvent)
def run(self):
self.trigger.emit()
def hotKeyEvent(self):
self.ui.screenshots_and_translate()
class MainFunction(Ui_MainWindow):
# 主程序
def __init__(self):
super().__init__()
self.trigger()
def trigger(self):
self.status = False
self.tray = Tray(self)
self.hotKey = HotKeyThread(self)
self.pushButton_2.setToolTip("截图(Ctrl+1)") # 给按钮2 添加提示气泡,显示快捷键方法
self.pushButton_1.clicked.connect(self.screenshots_and_translate) # 按钮1 翻译
self.pushButton_2.clicked.connect(self.screenshots_and_translate) # 按钮2 截图
def screenshots_and_translate(self):
butname = self.sender().objectName()
if butname == "pushButton1":
text = self.textedit.toPlainText()
res = None
if len(text) > 0:
res = translate(text)
self.textBrowser.setText(res)
else:
if not self.status:
self.status = True
self.showMinimized()
time.sleep(0.5)
self.activateWindow()
self.child_window = Child_Dialog(self) # 截图
self.child_window.dialogSignel.connect(self.slot_emit)
self.child_window.show()
def slot_emit(self, flag, str):
self.activateWindow()
self.showNormal()
self.status = False
if flag == 1:
if self.checkBox.isChecked():
res = translate(str)
else:
res = str
self.textBrowser.setText(res)
def closeEvent(self,QCloseEvent):
QCloseEvent.ignore()
self.hide()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainFunction()
sys.exit(app.exec_())