python3GUI--翻译器By:PyQt5(附源码)

文章目录

  • 一.准备工作
  • 二.预览
    • 1.启动
    • 2.选择语言
    • 3.翻译
  • 三.设计流程
    • 1.整体流程
    • 1.UI设计(草图)
    • 3.UI设计(QT设计师)
  • 四.源代码
    • 1. My_translator.py(主界面调动)
    • 2.translate_ui.py(主界面UI)
    • 3.language_select.py(语言选择窗口)
    • 4.about_author.py(关于作者 窗口)
    • 5.Translator.py(翻译器引擎)
  • 五.总结


前面用TK写过两款翻译器GUI,都是基于google_trans_new这个第三方模块写的,由于google_trans_new方法更新了,加之最近在学习PyQt5,在之前布局的基础上稍加改进,使用了一些QSS,设计了一下组件的布局,整体感觉比较清新小巧,开始~

一.准备工作

  1. 基于PyQt5的QT设计师,安装、配置详见:

PyCharm安装PyQt5及其工具(Qt Designer、PyUIC、PyRcc)详细教程

  1. 本次翻译仍然依赖第三方翻译模块:google_trans_new。
pip install google_trans_new

使用pip安装完成后,是不能直接用的,需要修改一行代码,详细操作参考

python3–最新google_trans_new超时报错JSONDecodeError解决

二.预览

1.启动

python3GUI--翻译器By:PyQt5(附源码)_第1张图片
整体采用垂直布局。

2.选择语言

python3GUI--翻译器By:PyQt5(附源码)_第2张图片

内置209种语言,默认自动选择翻译目标语言。

3.翻译

python3GUI--翻译器By:PyQt5(附源码)_第3张图片

这里我选择了英语语当做目标语言,翻译准确,速度快。

三.设计流程

1.整体流程

在这里插入图片描述

1.UI设计(草图)

python3GUI--翻译器By:PyQt5(附源码)_第4张图片

组件布局采用整体垂直布局,局部水平布局,支持窗口的缩放。

3.UI设计(QT设计师)

python3GUI--翻译器By:PyQt5(附源码)_第5张图片
有了草图,在QT设计师里设计界面就简单了。

四.源代码

1. My_translator.py(主界面调动)

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from Translator import Do_Trans
from about_author import About_Author_dialog
from language_select import Language_Select_Form
from translate_ui import Ui_MainWindow

class language_select_window(QWidget,Language_Select_Form):
    def __init__(self):
        # 信号的定义
        super().__init__()
        self.setupUi(self)


class about_author_window(QWidget,About_Author_dialog):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

class My_Translate(QMainWindow):
    message_single=pyqtSignal(int,str)
    def __init__(self):
        super(My_Translate, self).__init__()
        self.ui=Ui_MainWindow()
        self.ui.setupUi(self)
        self.init()
        self.ui.action_open_txt.triggered.connect(self.open_txt_file)
        self.ui.action_quit.triggered.connect(lambda :self.close())
        self.ui.action_translate.triggered.connect(self.do_translate)
        self.ui.action_select_aim_language.triggered.connect(self.select_aim_language)
        self.ui.action_clear_input.triggered.connect(self.clear_input)
        self.ui.action_copy_result.triggered.connect(self.copy_result)
        self.ui.action_set_font.triggered.connect(self.set_plaineidt_font)
        self.ui.action_about.triggered.connect(lambda :self.about_ui.show())
        self.ui.action_infos.triggered.connect(lambda :QMessageBox.information(self,'关于作者','作者:懷淰メ\nBy:PyQt5'))
        self.ui.pushButton.clicked.connect(self.clear_input)
        self.ui.pushButton_2.clicked.connect(self.do_translate)
        self.ui.pushButton_3.clicked.connect(self.copy_result)
        self.message_single.connect(self.show_message)


    def init(self):
        self.about_ui = about_author_window()
        self.lan_select_ui = language_select_window()
        self.current_aim_language = '自动选择'
        self.current_aim_language_short = 'auto'
        self.t = Do_Trans()
        language_table=self.t.get_language_table()
        self.languages=[lan['language'] for lan in language_table]
        self.language_table=[lan['short'] for lan in language_table]
        self.ui.label_3.setText(f'目标语言:{
       self.current_aim_language}')

    def set_plaineidt_font(self):
        font,ok=QFontDialog.getFont()
        if ok :
            self.ui.plainTextEdit.setFont(font)
            self.ui.plainTextEdit_2.setFont(font)

    @pyqtSlot(int,str)
    def show_message(self,type,message):
        """
        使用信号槽将messagebox信号处理
        :param type:
        :param message:
        :return:
        """
        if type==1:
            QMessageBox.information(self,"提示",message)
        elif type==2:
            QMessageBox.warning(self,"警告",message)
        elif type==3:
            QMessageBox.critical(self,"错误",message)

    def open_txt_file(self):
        """
        打开一个文本文件,将文本文件内容输入到输入框中
        :return:
        """
        aim_txt_file_path,_=QFileDialog.getOpenFileName(self,"打开一个文本文件",".",'文本文件(*.txt)')
        if aim_txt_file_path:
            file =open(aim_txt_file_path,'r',encoding='utf-8')
            data=file.readlines()
            file.close()
            self.ui.plainTextEdit.setPlainText(''.join(data))
        else:
            pass

    def do_translate(self):
        """
        翻译
        :return:
        """
        aim_trans=self.ui.plainTextEdit.toPlainText()
        if aim_trans=="":
            self.message_single.emit(1,'请输入内容!')
        else:
            self.ui.plainTextEdit_2.clear()
            trans_result=self.t.translate(aim_trans,self.current_aim_language_short)
            if trans_result:
                self.ui.plainTextEdit_2.setPlainText(trans_result)
            else:
                self.message_single.emit(3,'发生了一个错误!')

    def select_aim_language(self):
        """
        选择翻译目标语言
        :return:
        """
        current_language_index=self.language_table.index(self.current_aim_language_short)
        self.lan_select_ui.comboBox.addItems(self.languages)
        self.lan_select_ui.comboBox.setCurrentIndex(current_language_index)
        self.lan_select_ui.pushButton.clicked.connect(self.do_select_language)
        self.lan_select_ui.show()

    def do_select_language(self):
        """
        选择语言
        :return:
        """
        index=self.lan_select_ui.comboBox.currentIndex()
        self.current_aim_language=self.languages[index]
        self.current_aim_language_short=self.language_table[index]
        self.ui.label_3.setText(f'目标语言:{
       self.current_aim_language}')
        self.lan_select_ui.hide()

    def clear_input(self):
        """
        清空两个输入框
        :return:
        """
        self.ui.plainTextEdit.clear()
        self.ui.plainTextEdit_2.clear()
        self.ui.statusbar.showMessage("清空输入框成功!",3000)

    def copy_result(self):
        """
        复制翻译结果
        :return:
        """
        trans_result=self.ui.plainTextEdit_2.toPlainText()
        clipbord=QApplication.clipboard()
        clipbord.setText(trans_result)
        self.ui.statusbar.showMessage("复制翻译结果成功!",3000)


    def closeEvent(self,event):
        ret=QMessageBox.question(self,"退出",'确定要退出?',QMessageBox.Yes|QMessageBox.No,QMessageBox.Yes)
        if ret==QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


if __name__ == '__main__':
    app=QApplication(sys.argv)
    ui=My_Translate()
    ui.show()
    sys.exit(app.exec_())

2.translate_ui.py(主界面UI)

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'translate_ui.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(464, 505)
        MainWindow.setStyleSheet("QPushButton {\n"
"    border: 1px solid ;\n"
"    border-radius: 4px;\n"
"\n"
"    padding: 1px 4px;\n"
"    min-width: 60px;\n"
"    min-height: 23px;\n"
"}\n"
"\n"
"QPushButton:hover{\n"
"    background-color:rgb(255, 255, 255);\n"
"    border-color:rgb(255, 170, 0);\n"
"\n"
"}\n"
"\n"
"\n"
"\n"
"QPushButton:focus, QPushButton:default {\n"
"    border-color:rgb(0, 85, 255); /* make the default button prominent */\n"
"}\n"
"\n"
"\n"
"QPlainTextEdit{\n"
"    selection-background-color:rgb(255, 170, 127);\n"
"    border: 1px solid;\n"
"    border-style: inset;\n"
"}\n"
"QPlainTextEdit:hover{\n"
"    border-color:rgb(0, 170, 255);\n"
"}\n"
"QPlainTextEdit:focus{\n"
"    border-color: rgb(0, 170, 255);\n"
"}\n"
"\n"
"QLabel {\n"
"    background: transparent;\n"
"    border: 1px solid transparent;\n"
"    padding: 1px;\n"
"}\n"
"QComboBox{\n"
"    margin:2px;\n"
"    border: 1px solid ;\n"
"    border-radius: 4px;\n"
"    background-color:rgb(255, 255, 255);\n"
"    border-color: rgb(0, 0, 0);\n"
"    min-width: 180px;\n"
"    min-height: 20px;\n"
"}\n"
"QComboBox:hover{\n"
"    background-color:rgb(255, 255, 255);\n"
"    border-color: rgb(0, 170, 255);\n"
"\n"
"}\n"
"")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
        self.groupBox.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.groupBox.setAlignment(QtCore.Qt.AlignCenter)
        self.groupBox.setFlat(True)
        self.groupBox.setObjectName("groupBox")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.groupBox)
        self.verticalLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
        self.verticalLayout.setObjectName("verticalLayout")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(self.groupBox)
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.verticalLayout.addWidget(self.plainTextEdit)
        self.verticalLayout_3.addWidget(self.groupBox)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setObjectName("label_3")
        self.horizontalLayout.addWidget(self.label_3)
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem1)
        self.horizontalLayout.setStretch(0, 1)
        self.horizontalLayout.setStretch(1, 2)
        self.horizontalLayout.setStretch(2, 1)
        self.verticalLayout_3.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setContentsMargins(15, -1, 15, -1)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout_2.addWidget(self.pushButton)
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout_2.addWidget(self.pushButton_2)
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setObjectName("pushButton_3")
        self.horizontalLayout_2.addWidget(self.pushButton_3)
        self.verticalLayout_3.addLayout(self.horizontalLayout_2)
        self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_2.setAlignment(QtCore.Qt.AlignCenter)
        self.groupBox_2.setFlat(True)
        self.groupBox_2.setObjectName("groupBox_2")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.groupBox_2)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.plainTextEdit_2 = QtWidgets.QPlainTextEdit(self.groupBox_2)
        self.plainTextEdit_2.setObjectName("plainTextEdit_2")
        self.verticalLayout_2.addWidget(self.plainTextEdit_2)
        self.verticalLayout_3.addWidget(self.groupBox_2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 464, 26))
        self.menubar.setObjectName("menubar")
        self.menu = QtWidgets.QMenu(self.menubar)
        self.menu.setObjectName("menu")
        self.menu_2 = QtWidgets.QMenu(self.menubar)
        self.menu_2.setObjectName("menu_2")
        self.menu_3 = QtWidgets.QMenu(self.menubar)
        self.menu_3.setObjectName("menu_3")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.action_open_txt = QtWidgets.QAction(MainWindow)
        self.action_open_txt.setObjectName("action_open_txt")
        self.action_quit = QtWidgets.QAction(MainWindow)
        self.action_quit.setObjectName("action_quit")
        self.action_translate = QtWidgets.QAction(MainWindow)
        self.action_translate.setObjectName("action_translate")
        self.action_select_aim_language = QtWidgets.QAction(MainWindow)
        self.action_select_aim_language.setObjectName("action_select_aim_language")
        self.action_clear_input = QtWidgets.QAction(MainWindow)
        self.action_clear_input.setObjectName("action_clear_input")
        self.action_copy_result = QtWidgets.QAction(MainWindow)
        self.action_copy_result.setObjectName("action_copy_result")
        self.action_set_font = QtWidgets.QAction(MainWindow)
        self.action_set_font.setObjectName("action_set_font")
        self.action_about = QtWidgets.QAction(MainWindow)
        self.action_about.setObjectName("action_about")
        self.action_infos = QtWidgets.QAction(MainWindow)
        self.action_infos.setObjectName("action_infos")
        self.menu.addAction(self.action_open_txt)
        self.menu.addAction(self.action_quit)
        self.menu_2.addAction(self.action_translate)
        self.menu_2.addAction(self.action_select_aim_language)
        self.menu_2.addAction(self.action_clear_input)
        self.menu_2.addAction(self.action_copy_result)
        self.menu_2.addAction(self.action_set_font)
        self.menu_3.addAction(self.action_about)
        self.menu_3.addAction(self.action_infos)
        self.menubar.addAction(self.menu.menuAction())
        self.menubar.addAction(self.menu_2.menuAction())
        self.menubar.addAction(self.menu_3.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "My_translator"))
        self.groupBox.setTitle(_translate("MainWindow", "待翻译文本"))
        self.label_3.setText(_translate("MainWindow", "目标语言"))
        self.pushButton.setText(_translate("MainWindow", "清空输入框"))
        self.pushButton_2.setText(_translate("MainWindow", "翻译"))
        self.pushButton_3.setText(_translate("MainWindow", "复制翻译结果"))
        self.groupBox_2.setTitle(_translate("MainWindow", "翻译结果"))
        self.menu.setTitle(_translate("MainWindow", "开始"))
        self.menu_2.setTitle(_translate("MainWindow", "操作"))
        self.menu_3.setTitle(_translate("MainWindow", "关于"))
        self.action_open_txt.setText(_translate("MainWindow", "打开文本文件"))
        self.action_quit.setText(_translate("MainWindow", "退出"))
        self.action_translate.setText(_translate("MainWindow", "翻译"))
        self.action_select_aim_language.setText(_translate("MainWindow", "选择目标语言"))
        self.action_clear_input.setText(_translate("MainWindow", "清空输入框"))
        self.action_copy_result.setText(_translate("MainWindow", "复制翻译结果"))
        self.action_set_font.setText(_translate("MainWindow", "设置字体"))
        self.action_about.setText(_translate("MainWindow", "关于作者"))
        self.action_infos.setText(_translate("MainWindow", "关于软件"))

3.language_select.py(语言选择窗口)

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'language_select.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtWidgets


class Language_Select_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(374, 101)
        Form.setStyleSheet("QPushButton {\n"
"    border: 1px solid ;\n"
"    border-radius: 4px;\n"
"\n"
"    padding: 1px 4px;\n"
"    min-width: 80px;\n"
"    min-height: 20px;\n"
"}\n"
"\n"
"QPushButton:hover{\n"
"    background-color:rgb(255, 255, 255);\n"
"    border-color: rgb(0, 170, 255);\n"
"\n"
"}\n"
"\n"
"QPushButton:pressed\n"
"{\n"
"    border-width: 1px;      \n"
"    background-color:rgb(255, 255, 255);\n"
"    border-color: rgb(0, 170, 255);\n"
"}\n"
"\n"
"QPushButton:focus, QPushButton:default {\n"
"    border-color:rgb(0, 85, 255); /* make the default button prominent */\n"
"}\n"
"\n"
"\n"
"QPlainTextEdit{\n"
"    selection-background-color:rgb(255, 170, 127);\n"
"    border: 1px solid;\n"
"    border-style: inset;\n"
"}\n"
"QPlainTextEdit:hover{\n"
"    border-color:rgb(0, 170, 255);\n"
"}\n"
"QPlainTextEdit:focus{\n"
"    border-color: rgb(0, 170, 255);\n"
"}\n"
"\n"
"QLabel {\n"
"    background: transparent;\n"
"    border: 1px solid transparent;\n"
"    padding: 1px;\n"
"}\n"
"QComboBox{\n"
"    margin:2px;\n"
"    border: 1px solid ;\n"
"    border-radius: 4px;\n"
"    background-color:rgb(255, 255, 255);\n"
"    border-color: rgb(0, 0, 0);\n"
"    min-width: 180px;\n"
"    min-height: 20px;\n"
"}\n"
"QComboBox:hover{\n"
"    background-color:rgb(255, 255, 255);\n"
"    border-color: rgb(0, 170, 255);\n"
"\n"
"}\n"
"")
        self.verticalLayout = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout.setObjectName("verticalLayout")
        self.groupBox = QtWidgets.QGroupBox(Form)
        self.groupBox.setObjectName("groupBox")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.groupBox)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.comboBox = QtWidgets.QComboBox(self.groupBox)
        self.comboBox.setObjectName("comboBox")
        self.horizontalLayout.addWidget(self.comboBox)
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem1)
        self.pushButton = QtWidgets.QPushButton(self.groupBox)
        self.pushButton.setMaximumSize(QtCore.QSize(100, 16777215))
        self.pushButton.setBaseSize(QtCore.QSize(40, 20))
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.horizontalLayout.setStretch(0, 2)
        self.horizontalLayout.setStretch(1, 3)
        self.horizontalLayout_2.addLayout(self.horizontalLayout)
        self.verticalLayout.addWidget(self.groupBox)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "语言选择"))
        self.groupBox.setTitle(_translate("Form", "请选择一种语言"))
        self.pushButton.setText(_translate("Form", "选择"))

4.about_author.py(关于作者 窗口)

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'about_author.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class About_Author_dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.setWindowModality(QtCore.Qt.ApplicationModal)
        Dialog.resize(340, 249)
        Dialog.setStyleSheet("QPushButton {\n"
"    border: 1px solid ;\n"
"    border-radius: 4px;\n"
"\n"
"    padding: 1px 4px;\n"
"    min-width: 80px;\n"
"    min-height: 20px;\n"
"}\n"
"\n"
"QPushButton:hover{\n"
"    background-color:rgb(255, 255, 255);\n"
"    border-color: rgb(0, 170, 255);\n"
"\n"
"}\n"
"\n"
"QPushButton:pressed\n"
"{\n"
"    border-width: 1px;      \n"
"    background-color:rgb(255, 255, 255);\n"
"    border-color: rgb(0, 170, 255);\n"
"}\n"
"\n"
"QPushButton:focus, QPushButton:default {\n"
"    border-color:rgb(0, 85, 255); /* make the default button prominent */\n"
"}\n"
"\n"
"\n"
"QPlainTextEdit{\n"
"    selection-background-color:rgb(255, 170, 127);\n"
"    border: 1px solid;\n"
"    border-style: inset;\n"
"}\n"
"QPlainTextEdit:hover{\n"
"    border-color:rgb(0, 170, 255);\n"
"}\n"
"QPlainTextEdit:focus{\n"
"    border-color: rgb(0, 170, 255);\n"
"}\n"
"\n"
"QLabel {\n"
"    background: transparent;\n"
"    border: 1px solid transparent;\n"
"    padding: 1px;\n"
"}\n"
"")
        self.layoutWidget = QtWidgets.QWidget(Dialog)
        self.layoutWidget.setGeometry(QtCore.QRect(30, 30, 271, 141))
        self.layoutWidget.setObjectName("layoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label_2 = QtWidgets.QLabel(self.layoutWidget)
        self.label_2.setText("")
        self.label_2.setPixmap(QtGui.QPixmap("head_img.png"))
        self.label_2.setObjectName("label_2")
        self.horizontalLayout.addWidget(self.label_2)
        self.label = QtWidgets.QLabel(self.layoutWidget)
        self.label.setStyleSheet("QLabel{\n"
"    \n"
"    font: 16pt \"Arial\";\n"
"}")
        self.label.setTextFormat(QtCore.Qt.MarkdownText)
        self.label.setOpenExternalLinks(True)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(100, 200, 90, 24))
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(Dialog)
        self.pushButton.clicked.connect(Dialog.close)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "关于作者"))
        self.label.setText(_translate("Dialog", "

作者:懷淰メ

By:PyQt5

作者主页

"
)) self.pushButton.setText(_translate("Dialog", "OK"))

5.Translator.py(翻译器引擎)

from google_trans_new import google_translator
import re

class Do_Trans:
    def get_language_table(self,):
        lan_table1 = lan_table.strip().replace("'", '')
        names = re.findall(r'[\u4e00-\u9fa5()]+', lan_table1)
        lans = re.findall(r'[^\u4e00-\u9fa5 \s:()]+', lan_table1)
        language_table = []
        for language in zip(names, lans):
            item = {
     }
            item['language'] = language[0]
            item['short'] = language[1]
            language_table.append(item)
        return language_table

    def translate(self,text,language):
        try:
            t=google_translator().translate(text,language)
            return t
        except:
            return False
lan_table='''
'自动选择': 'auto'
'南非荷兰语': 'af'
'阿尔巴尼亚语': 'sq'
'阿姆哈拉语': 'am'
'阿拉伯语': 'ar'
'亚美尼亚语': 'hy'
'阿塞拜疆语': 'az'
'巴斯克语': 'eu'
'白俄罗斯语': 'be'
'孟加拉语': 'bn'
'波斯尼亚语': 'bs'
'保加利亚语': 'bg'
'加泰罗尼亚语': 'ca'
'宿务语': 'ceb'
'中文(简体)': 'zh'
'中文(繁体)': 'zh-TW'
'科西嘉语': 'co'
'克罗地亚语': 'hr'
'捷克语': 'cs'
'丹麦语': 'da'
'荷兰语': 'nl'
'英语': 'en'
'世界语': 'eo'
'爱沙尼亚语': 'et'
'芬兰语': 'fi'
'法语': 'fr'
'弗里西语': 'fy'
'加利西亚语': 'gl'
'格鲁吉亚语': 'ka'
'德语': 'de'
'希腊语': 'el'
'古吉拉特语': 'gu'
'海地克里奥尔语': 'ht'
'豪萨语': 'ha'
'夏威夷语': 'haw'
'希伯来语': 'he'
'印地语': 'hi'
'苗语': 'hmn'
'匈牙利语': 'hu'
'冰岛语': 'is'
'伊博语': 'ig'
'印尼语': 'id'
'爱尔兰语': 'ga'
'意大利语': 'it'
'日语': 'ja'
'爪哇语': 'jw'
'卡纳达语': 'kn'
'哈萨克语': 'kk'
'高棉语': 'km'
'韩语': 'ko'
'库尔德语': 'ku'
'吉尔吉斯语': 'ky'
'老挝语': 'lo'
'拉丁语': 'la'
'拉脱维亚语': 'lv'
'立陶宛语': 'lt'
'卢森堡语': 'lb'
'马其顿语': 'mk'
'马尔加什语': 'mg'
'马来语': 'ms'
'马拉雅拉姆语': 'ml'
'马耳他语': 'mt'
'毛利语': 'mi'
'马拉地语': 'mr'
'蒙古语': 'mn'
'缅甸语': 'my'
'尼泊尔语': 'ne'
'挪威语': 'no'
'尼杨扎语(齐切瓦语)': 'ny'
'普什图语': 'ps'
'波斯语': 'fa'
'波兰语': 'pl'
'葡萄牙语': 'pt'
'旁遮普语': 'pa'
'罗马尼亚语': 'ro'
'俄语': 'ru'
'萨摩亚语': 'sm'
'苏格兰盖尔语': 'gd'
'塞尔维亚语': 'sr'
'塞索托语': 'st'
'修纳语': 'sn'
'信德语': 'sd'
'僧伽罗语': 'si'
'斯洛伐克语': 'sk'
'斯洛文尼亚语': 'sl'
'索马里语': 'so'
'西班牙语': 'es'
'巽他语': 'su'
'斯瓦希里语': 'sw'
'瑞典语': 'sv'
'塔加洛语(菲律宾语)': 'tl'
'塔吉克语': 'tg'
'泰米尔语': 'ta'
'泰卢固语': 'te'
'泰语': 'th'
'土耳其语': 'tr'
'乌克兰语': 'uk'
'乌尔都语': 'ur'
'乌兹别克语': 'uz'
'越南语': 'vi'
'威尔士语': 'cy'
'班图语': 'xh'
'意第绪语': 'yi'
'约鲁巴语': 'yo'
'祖鲁语': 'zu'
'南非荷兰语': 'af'
'阿尔巴尼亚语': 'sq'
'阿姆哈拉语': 'am'
'阿拉伯语': 'ar'
'亚美尼亚语': 'hy'
'阿塞拜疆语': 'az'
'巴斯克语': 'eu'
'白俄罗斯语': 'be'
'孟加拉语': 'bn'
'波斯尼亚语': 'bs'
'保加利亚语': 'bg'
'加泰罗尼亚语': 'ca'
'宿务语': 'ceb'
'中文(简体)': 'zh'
'中文(繁体)': 'zh-TW'
'科西嘉语': 'co'
'克罗地亚语': 'hr'
'捷克语': 'cs'
'丹麦语': 'da'
'荷兰语': 'nl'
'英语': 'en'
'世界语': 'eo'
'爱沙尼亚语': 'et'
'芬兰语': 'fi'
'法语': 'fr'
'弗里西语': 'fy'
'加利西亚语': 'gl'
'格鲁吉亚语': 'ka'
'德语': 'de'
'希腊语': 'el'
'古吉拉特语': 'gu'
'海地克里奥尔语': 'ht'
'豪萨语': 'ha'
'夏威夷语': 'haw'
'希伯来语': 'he'
'印地语': 'hi'
'苗语': 'hmn'
'匈牙利语': 'hu'
'冰岛语': 'is'
'伊博语': 'ig'
'印尼语': 'id'
'爱尔兰语': 'ga'
'意大利语': 'it'
'日语': 'ja'
'爪哇语': 'jw'
'卡纳达语': 'kn'
'哈萨克语': 'kk'
'高棉语': 'km'
'韩语': 'ko'
'库尔德语': 'ku'
'吉尔吉斯语': 'ky'
'老挝语': 'lo'
'拉丁语': 'la'
'拉脱维亚语': 'lv'
'立陶宛语': 'lt'
'卢森堡语': 'lb'
'马其顿语': 'mk'
'马尔加什语': 'mg'
'马来语': 'ms'
'马拉雅拉姆语': 'ml'
'马耳他语': 'mt'
'毛利语': 'mi'
'马拉地语': 'mr'
'蒙古语': 'mn'
'缅甸语': 'my'
'尼泊尔语': 'ne'
'挪威语': 'no'
'尼杨扎语(齐切瓦语)': 'ny'
'普什图语': 'ps'
'波斯语': 'fa'
'波兰语': 'pl'
'葡萄牙语': 'pt'
'旁遮普语': 'pa'
'罗马尼亚语': 'ro'
'俄语': 'ru'
'萨摩亚语': 'sm'
'苏格兰盖尔语': 'gd'
'塞尔维亚语': 'sr'
'塞索托语': 'st'
'修纳语': 'sn'
'信德语': 'sd'
'僧伽罗语': 'si'
'斯洛伐克语': 'sk'
'斯洛文尼亚语': 'sl'
'索马里语': 'so'
'西班牙语': 'es'
'巽他语': 'su'
'斯瓦希里语': 'sw'
'瑞典语': 'sv'
'塔加洛语(菲律宾语)': 'tl'
'塔吉克语': 'tg'
'泰米尔语': 'ta'
'泰卢固语': 'te'
'泰语': 'th'
'土耳其语': 'tr'
'乌克兰语': 'uk'
'乌尔都语': 'ur'
'乌兹别克语': 'uz'
'越南语': 'vi'
'威尔士语': 'cy'
'班图语': 'xh'
'意第绪语': 'yi'
'约鲁巴语': 'yo'
'祖鲁语': 'zu'
'''

五.总结

本次使用PyQt5制作了一款简单小巧的翻译器,支持超过200种语言的翻译,同时也支持文本文件翻译,在测试中没有发现什么问题,使用QSS对部分组件设计了样式,加了伪状态,用户体验有所提升,翻译器的话不打算再做下去了,但是PyQt5的路我还要一直走下去。程序打包好放在了蓝奏云。思路、代码方面有什么不足欢迎各位大佬指正、批评!能点个赞给我个鼓励吗?
python3GUI--翻译器By:PyQt5(附源码)_第6张图片

你可能感兴趣的:(GUI,PyQt5,python小工具,python,qt,pycharm)