Skip to content

Commit

Permalink
Google Translate 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Tich committed Jul 16, 2018
1 parent 3ad1521 commit c11bfbc
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 11 deletions.
78 changes: 72 additions & 6 deletions google-translate/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,91 @@
# @Version : Python3.5

from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QWidget, QApplication
from mwin import Ui_MWin
import sys

class MWin(Ui_MWin):
def __init__(self, parent=None):
super(MWin, self).__init__()
class Dialog(QWidget):
"""对QWidget类重写,实现一些功能"""
def __init__(self):
super().__init__()

# 检测键盘回车按键
def keyPressEvent(self, event):
print("按下:" + str(event.key()))
# 举例
if(event.key() == QtCore.Qt.Key_Escape):
print('测试:ESC')
if(event.key() == QtCore.Qt.Key_A):
print('测试:A')
if(event.key() == QtCore.Qt.Key_1):
print('测试:1')
if(event.key() == QtCore.Qt.Key_Enter):
print('测试:Enter')
if(event.key() == QtCore.Qt.Key_Space):
print('测试:Space')
if (event.key() == QtCore.Qt.Key_H) and QApplication.keyboardModifiers() == QtCore.Qt.ControlModifier:
# self.box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.about, "谷歌翻译App", "用户名和密码不匹配!")
# qyes=self.box.addButton("确定", QtWidgets.QMessageBox.YesRole)
# # qno=self.box.addButton("取消", QtWidgets.QMessageBox.NoRole)
# self.box.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
# self.box.show()
self.reply = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, '谷歌翻译App v1.1',
"1、输入翻译。快捷键 Ctrl+Enter 或点击按钮 \
2、论文模式。需同时勾选实时翻译(监控剪贴板),会自动将回车和多个空格替换为一个空格,以及去掉一个特殊的符号 � \
\n3、窗口置顶。即应用始终在桌面顶层。 " )
self.reply.addButton(" 确 定 ", QtWidgets.QMessageBox.YesRole)
self.reply.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.reply.setIconPixmap(QtGui.QPixmap(":/images/icon64"))
try:
with open('style.qss') as f:
style = f.read() # 读取样式表
self.reply.setStyleSheet(style)
except:
print("open stylesheet error")
self.reply.show()
# reply = QtWidgets.QMessageBox.about(self,
# '谷歌翻译App',
# "1、输入翻译。快捷键 Ctrl+Enter 或点击按钮 \
# 2、论文模式。需同时勾选实时翻译(监控剪贴板),会自动将回车和多个空格替换为一个空格,以及去掉一个特殊的符号 � \
# \n3、窗口置顶。即应用始终在桌面顶层。 "
# )

def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
print("鼠标左键点击")
elif event.button() == QtCore.Qt.RightButton:
print("鼠标右键点击")
elif event.button() == QtCore.Qt.MidButton:
print("鼠标中键点击")

# def closeEvent(self, event):
# """
# 重写closeEvent方法,实现dialog窗体关闭时执行一些代码
# :param event: close()触发的事件
# :return: None
# """
# reply = QtWidgets.QMessageBox.question(self,
# '谷歌翻译App',
# "是否要退出程序?",
# QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
# QtWidgets.QMessageBox.No)
# if reply == QtWidgets.QMessageBox.Yes:
# event.accept()
# else:
# event.ignore()

if __name__ == '__main__':
app = QApplication(sys.argv)
base = QWidget() # 创建基本窗口
base = Dialog() # 创建基本窗口
try:
with open('style.qss') as f:
style = f.read() # 读取样式表
base.setStyleSheet(style)
except:
print("open stylesheet error")
w = MWin() # 创建用户界面类的实例
w = Ui_MWin() # 创建用户界面类的实例
w.setupUi(base) # 将用户界面加载到基本窗口
# 监控剪贴板
clipboard = QApplication.clipboard()
Expand Down
59 changes: 54 additions & 5 deletions google-translate/mwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget
from utils import trans_To_zh_CN
from PyQt5.QtCore import QTimer, QThread, pyqtSignal
# from utils import trans_To_zh_CN
import re
from googletrans import Translator

GTransData = ''

class Ui_MWin(QWidget):
def __init__(self):
Expand Down Expand Up @@ -132,9 +136,10 @@ def setupUi(self, MWin):

def retranslateUi(self, MWin):
_translate = QtCore.QCoreApplication.translate
MWin.setWindowTitle(_translate("MWin", "谷歌翻译App"))
MWin.setWindowTitle(_translate("MWin", "谷歌翻译App v1.1"))
self.originLabel.setText(_translate("MWin", "原文:"))
self.transLabel.setText(_translate("MWin", "翻译:"))
self.originText.setPlaceholderText(_translate("MWin", "Ctrl+h 获取帮助"))
self.realTimeTrans.setText(_translate("MWin", "实时翻译"))
self.paperMode.setText(_translate("MWin", "论文模式"))
self.alwaysFront.setText(_translate("MWin", "窗口置顶"))
Expand All @@ -159,7 +164,12 @@ def transTextToZhCN(self):
text = self.originText.toPlainText()
if text:
try:
self.transText.setPlainText(trans_To_zh_CN(text))
# self.transText.setPlainText(trans_To_zh_CN(text))
self.t=GTranslator(text)
self.t.start()
self.transText.setPlainText("")
self.transText.setPlaceholderText("翻译中...")
self.t.trigger.connect(self.translated)
except:
self.transText.setPlainText("翻译出错!")

Expand Down Expand Up @@ -196,8 +206,47 @@ def onClipboradChanged(self):
content = re.sub(r'', '', content)
self.originText.setPlainText(content)
try:
data = trans_To_zh_CN(content)
self.transText.setPlainText(data)
# data = trans_To_zh_CN(content)
# self.transText.setPlainText(data)
self.t=GTranslator(content)
self.t.start()
self.transText.setPlainText("")
self.transText.setPlaceholderText("翻译中...")
self.t.trigger.connect(self.translated)
except:
self.transText.setPlainText("翻译出错!")

def translated(self):
global GTransData
if GTransData:
self.transText.setPlainText(GTransData)
else:
self.transText.setPlainText("翻译出错!")
GTransData = ""

class GTranslator(QThread):
trigger = pyqtSignal()
def __init__(self, content):
super().__init__()
self.content = content

def run(self):
"""
将origin翻译成中文,origin可以是一个字符串,也可以是一个列表
"""
Data = []
global GTransData
T = Translator(service_urls=['translate.google.cn'])
# ts = T.translate(['The quick brown fox', 'jumps over', 'the lazy dog'], dest='zh-CN')
# print('原文', origin)
ts = T.translate(self.content, dest='zh-CN')
# print('翻译后',ts.text)
if isinstance(ts.text, list):
for i in ts:
Data.append(i.text)
GTransData = Data
else:
GTransData = ts.text
self.trigger.emit() # 翻译完毕后发出信号

import res_rc
Binary file modified images/Google-Translate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c11bfbc

Please sign in to comment.