新开一个线程去翻译,如下
class CGoogleTransThread(QThread):
threadTrans = pyqtSignal(str)
def __init__(self, sDestTrans, sContent):
super(CGoogleTransThread, self).__init__()
self.m_sDestTrans = sDestTrans
self.m_sContent = sContent
def run(self):
lstData = []
sResultData = ""
oTran = Translator(service_urls=["translate.google.cn"])
# ts = T.translate(['The quick brown fox', 'jumps over', 'the lazy dog'], dest='zh-CN')
try:
oText = oTran.translate(self.m_sContent, dest=self.m_sDestTrans)
if isinstance(oText.text, list):
for i in oText:
lstData.append(i.text)
sResultData = " ".join(lstData)
else:
sResultData = oText.text
except:
sResultData = "An error happended. Please retry..."
self.threadTrans.emit(sResultData) # emit signal once translatation is finfished
使用
self.m_oThreadTrans = CGoogleTransThread(self.m_sDestLan, sOriText)
self.m_oThreadTrans.threadTrans.connect(self._finishedTrans)
self.m_oThreadTrans.start()
首先生产qm文件
在这里插入代码片 # 设置当前语言
def changeCurLan(self, iSetLan):
import os
sBase = os.path.abspath(".")
if iSetLan == 0 and self.m_iLanType != 0:
self.m_iLanType = 0
print("[ChangeLan] --> zh_CN")
sPath = os.path.join(sBase, "../", "zh_CN.qm")
self.m_oCurLan.load(sPath)
elif iSetLan == 1 and self.m_iLanType != 1:
self.m_iLanType = 1
print("[ChangeLan] --> English")
sPath = os.path.join(sBase, "../", "en.qm")
bLoadResult = self.m_oCurLan.load(sPath)
print("loadresult", bLoadResult)
else:
return
_app = QApplication.instance()
bResult = _app.installTranslator(self.m_oCurLan)
print("installresult", bResult)
self.retranslateUi(self)
def _initStyle(self):
try:
with open("../Res/style.qss") as f:
style = f.read() # 读取样式表
self.setStyleSheet(style)
except:
print("open stylesheet error")
# 从qrc文件中读取
def _initStyle(self):
qss_file = QFile(":Res/style.qss")
qss_file.open(QFile.ReadOnly)
qss = str(qss_file.readAll(), encoding='utf-8')
qss_file.close()
self.setStyleSheet(qss)
def openUrl(self, sUrl):
QDesktopServices.openUrl(
QUrl(sUrl)
)
# 打开文件,读取数据
def openFile(self):
sFileName, sFileType = QFileDialog.getOpenFileName(self, "Open File", ".")
if sFileName:
print("Open: ", sFileName, sFileType)
with open(sFileName, encoding="utf-8") as f:
try:
self.txtEditOri.setPlainText(str(f.read()))
except Exception as e:
self.txtEditOri.setPlainText(e.args[1])
# 导出数据到文件
def exportFile(self):
sTransTxt = self.txtEditTrans.toPlainText()
if not sTransTxt:
return
sFileName, sFileType = QFileDialog.getSaveFileName(self, "Save File", ".", "Text files(*.txt);;All files(*.*)")
if sFileName:
with open(sFileName, "w", encoding="utf-8") as f:
try:
f.write(sTransTxt)
except Exception as e:
self.txtEditTrans.setPlainText(e.args[1])
# 窗口置顶
def setWindowTop(self, bTop):
if bTop:
self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
self.show()
else:
self.setWindowFlags(Qt.Widget)
self.show()
def _copyResultText(self):
sText = self.txtEditTrans.toPlainText()
if not sText:
return
clipboard = QApplication.clipboard()
clipboard.setText(sText)
def Center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def _initSysPath(self):
import os
import sys
lstPath = [
"qrcpy", # 要写这个,否则qrc导出的py,import时找不到模块
]
sBase = os.path.abspath(".")
print("code base path: ", sBase)
for sPath in lstPath:
sFullPath = os.path.join(sBase, sPath)
print("AppendPath: ", sFullPath)
sys.path.append(sFullPath)
CAppInit()
def Start():
import dlgmain
dlgmain.Start()
Start()
github源码
打成exe遇到qrc的pyimport失败的问题,在main.spec中增加路径就好
然后使用该spec来制作exe
pyinstaller -F -c main.spec
对于pyqt的一些提示,目前是import *,日后若pyqt可以自动提示,这里可以修改一下
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *