自动翻译器2
2019-08-05 本文已影响0人
技术喵
自动翻译器的qt部分
接下来我们要实现qt窗口部分,这里遇到一个很尴尬的事情,qt for python的开发环境要求按照python,但我安装的是Anaconda,使用Jupyter开发,安完了PySide2,Qt找不到这个模块,用Jupyter呢,又提示找不到qt.qpa.plugin,打开环境变量查看os.environ,
'QT_QPA_PLATFORM_PLUGIN_PATH': 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\PySide2\\plugins\\platform'
platform竟然少个s,我也是醉了,由于不想在py文件追加环境变量配置,我很机智的把文件夹platforms复制了一份,改名platform,这样是不好的,但是好用呀。
- 追加main.py文件,导入需要的包
import sys
import random
from PySide2.QtWidgets import QApplication, QWidget, QTextEdit, QHBoxLayout
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile, QDir, QTimer, Slot, Qt
from PySide2.QtGui import QClipboard
from Spider import *
- 使用线程创建翻译器
#创建翻译器
class WorkerThread(QThread):
resultReady = Signal()
def __init__(self):
super().__init__()
def __del__(self):
pass
def run(self):
global translation
translation = Translation()
self.resultReady.emit()
- 翻译过程也使用线程
#翻译线程
class WorkerThreadTrans(QThread):
resultReady = Signal(str)
def __init__(self):
super().__init__()
self.last = ''
def setText(self, value):
self.value = value
def run(self):
self.result = ''
while(1):
self.getClipborad()
time.sleep(1)
@Slot()
def getClipborad(self):
global translation
board = QApplication.clipboard()
self.str = board.text()
if self.str == '' or self.str == self.last:
pass
else:
try:
self.last = self.str
self.result = translation.translate(self.str)
except:
pass
finally:
pass
self.resultReady.emit(self.result)
- 实现窗口类
#窗口
class Form(QWidget):
def __init__(self):
super().__init__()
self.textEdit = QTextEdit()
self.layout = QHBoxLayout()
self.layout.addWidget(self.textEdit)
self.setLayout(self.layout)
self.setWindowTitle("百度翻译器")
self.resize(400, 300)
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.icon = QIcon()
self.icon.addPixmap(QPixmap('favicon.ico'), QIcon.Normal, QIcon.Off)
self.setWindowIcon(self.icon)
self.create = False
self.thread = WorkerThread()
self.thread.finished.connect(self.thread.deleteLater)
self.thread.resultReady.connect(self.createTrans)
self.thread.start()
self.threadTrans = WorkerThreadTrans()
self.threadTrans.finished.connect(self.threadTrans.deleteLater)
self.threadTrans.resultReady.connect(self.setTextValue)
def __del__(self):
self.threadTrans.quit()
self.threadTrans.wait()
if self.create:
del translation
@Slot()
def createTrans(self):
self.create = True
self.threadTrans.start()
@Slot()
def setTextValue(self, value):
self.textEdit.setPlainText(value)
- main
if __name__ == "__main__":
app = QApplication(sys.argv)
translation = None
window = Form()
window.show()
sys.exit(app.exec_())
为了防止窗口启动卡顿,运行卡顿,我们开辟了两个线程。我们还把窗口置顶,为窗口设置了图标。我们重载了窗口关闭事件,用于把线程关闭。
-
程序运行效果
1564980342307.png -
打包
具体打包操作请看帖子 https://www.jianshu.com/p/046e690c0f12
打包命令
pyinstaller -F -w -i favicon.ico --icon=favicon.ico main.py -p C:\ProgramData\Anaconda3\Lib\site-packages\PySide2
为了平台显示我们把C:\ProgramData\Anaconda3\Lib\site-packages\PySide2\plugins\platforms\qwindows.dll也拷贝过来
1564993349890.png
- 去除selenium隐藏控制台解决办法
参考帖子 https://www.cnblogs.com/TurboWay/p/9300105.html
修改C:\ProgramData\Anaconda3\Lib\site-packages\selenium\webdriver\common\service.py源码