自動翻譯器的qt部分
接下來我們要實現(xiàn)qt窗口部分博烂,這里遇到一個很尷尬的事情香椎,qt for python的開發(fā)環(huán)境要求按照python,但我安裝的是Anaconda禽篱,使用Jupyter開發(fā)畜伐,安完了PySide2,Qt找不到這個模塊躺率,用Jupyter呢玛界,又提示找不到qt.qpa.plugin陨仅,打開環(huán)境變量查看os.environ立轧,
'QT_QPA_PLATFORM_PLUGIN_PATH': 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\PySide2\\plugins\\platform'
platform竟然少個s欢际,我也是醉了稼跳,由于不想在py文件追加環(huán)境變量配置鸟雏,我很機(jī)智的把文件夾platforms復(fù)制了一份物臂,改名platform瘟滨,這樣是不好的锯蛀,但是好用呀。
- 追加main.py文件馅精,導(dǎo)入需要的包
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 *
- 使用線程創(chuàng)建翻譯器
#創(chuàng)建翻譯器
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)
- 實現(xiàn)窗口類
#窗口
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_())
為了防止窗口啟動卡頓严嗜,運(yùn)行卡頓,我們開辟了兩個線程洲敢。我們還把窗口置頂阻问,為窗口設(shè)置了圖標(biāo)。我們重載了窗口關(guān)閉事件沦疾,用于把線程關(guān)閉称近。
-
程序運(yùn)行效果
打包
具體打包操作請看帖子 http://www.reibang.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也拷貝過來
- 去除selenium隱藏控制臺解決辦法
參考帖子 https://www.cnblogs.com/TurboWay/p/9300105.html
修改C:\ProgramData\Anaconda3\Lib\site-packages\selenium\webdriver\common\service.py源碼