截圖可以使用第三方截圖 或 使用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_())