VSC+python+mysql,
文章目录
目录
文章目录
前言
一、详细代码
第一个文件Ui_untitled.py
第二个fy_main.py文件:
第三个 :bd_pachong.py
二、登录界面:
总结
前言
通过pyqt5实现了翻译界面的可视化,利用python爬虫实现了翻译,如下图:
英——>汉
汉——>英
句子翻译:
一、详细代码
第一个文件Ui_untitled.py
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'd:\python代码\翻译\untitled.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.QtWidgets import QApplication,QMainWindow,QMessageBox
from PyQt5 import QtCore, QtGui, QtWidgets
from bdpachong import *class Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(900, 741)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(255, 255, 255, 255), stop:1 rgba(255, 255, 255, 255));")self.centralwidget.setObjectName("centralwidget")self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)self.verticalLayout.setContentsMargins(0, 0, 0, 0)self.verticalLayout.setSpacing(1)self.verticalLayout.setObjectName("verticalLayout")self.textEdit = QtWidgets.QTextEdit(self.centralwidget)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.textEdit.sizePolicy().hasHeightForWidth())self.textEdit.setSizePolicy(sizePolicy)self.textEdit.setMaximumSize(QtCore.QSize(16777215, 85))self.textEdit.setSizeIncrement(QtCore.QSize(0, 0))font = QtGui.QFont()font.setPointSize(20)font.setBold(True)font.setWeight(75)font.setKerning(True)self.textEdit.setFont(font)self.textEdit.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.ArrowCursor))self.textEdit.setMouseTracking(False)self.textEdit.setToolTipDuration(-1)self.textEdit.setStyleSheet("background-color: qlineargradient(spread:pad, x1:1, y1:1, x2:1, y2:1, stop:0 rgba(4, 192, 213, 255), stop:1 rgba(255, 255, 255, 255));\n"
"border-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(255, 255, 255, 255), stop:1 rgba(255, 255, 255, 255));")self.textEdit.setFrameShadow(QtWidgets.QFrame.Raised)self.textEdit.setLineWidth(1)self.textEdit.setMidLineWidth(0)self.textEdit.setLineWrapColumnOrWidth(0)self.textEdit.setTabStopWidth(80)self.textEdit.setAcceptRichText(True)self.textEdit.setCursorWidth(1)self.textEdit.setObjectName("textEdit")self.verticalLayout.addWidget(self.textEdit)self.textEdit_2 = QtWidgets.QTextEdit(self.centralwidget)self.textEdit_2.setMaximumSize(QtCore.QSize(16777215, 60))self.textEdit_2.setStyleSheet("background-color: qlineargradient(spread:pad, x1:1, y1:0, x2:1, y2:1, stop:0 rgba(0, 192, 213, 255), stop:1 rgba(190, 255, 255, 255));")self.textEdit_2.setObjectName("textEdit_2")self.verticalLayout.addWidget(self.textEdit_2)self.pushButton = QtWidgets.QPushButton(self.centralwidget)palette = QtGui.QPalette()brush = QtGui.QBrush(QtGui.QColor(147, 221, 221))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)brush = QtGui.QBrush(QtGui.QColor(170, 255, 255))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)brush = QtGui.QBrush(QtGui.QColor(170, 255, 255))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)brush = QtGui.QBrush(QtGui.QColor(170, 255, 255))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)brush = QtGui.QBrush(QtGui.QColor(147, 221, 221))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)brush = QtGui.QBrush(QtGui.QColor(170, 255, 255))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)brush = QtGui.QBrush(QtGui.QColor(170, 255, 255))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)brush = QtGui.QBrush(QtGui.QColor(170, 255, 255))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)brush = QtGui.QBrush(QtGui.QColor(170, 255, 255))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)brush = QtGui.QBrush(QtGui.QColor(170, 255, 255))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)brush = QtGui.QBrush(QtGui.QColor(170, 255, 255))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)self.pushButton.setPalette(palette)font = QtGui.QFont()font.setPointSize(10)font.setBold(True)font.setItalic(False)font.setWeight(75)self.pushButton.setFont(font)self.pushButton.setStyleSheet("background-color: rgb(170, 255, 255);\n"
"border-color: rgb(170, 255, 255);\n"
"")self.pushButton.setObjectName("pushButton")self.verticalLayout.addWidget(self.pushButton)self.label_5 = QtWidgets.QLabel(self.centralwidget)self.label_5.setMaximumSize(QtCore.QSize(16777215, 30))palette = QtGui.QPalette()brush = QtGui.QBrush(QtGui.QColor(186, 186, 186))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)brush = QtGui.QBrush(QtGui.QColor(222, 222, 222, 222))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)brush = QtGui.QBrush(QtGui.QColor(222, 222, 222, 222))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)brush = QtGui.QBrush(QtGui.QColor(222, 222, 222, 222))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)brush = QtGui.QBrush(QtGui.QColor(186, 186, 186))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)brush = QtGui.QBrush(QtGui.QColor(222, 222, 222, 222))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)brush = QtGui.QBrush(QtGui.QColor(222, 222, 222, 222))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)brush = QtGui.QBrush(QtGui.QColor(222, 222, 222, 222))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)brush = QtGui.QBrush(QtGui.QColor(222, 222, 222, 222))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)brush = QtGui.QBrush(QtGui.QColor(222, 222, 222, 222))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)brush = QtGui.QBrush(QtGui.QColor(222, 222, 222, 222))brush.setStyle(QtCore.Qt.SolidPattern)palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)self.label_5.setPalette(palette)font = QtGui.QFont()font.setBold(True)font.setWeight(75)self.label_5.setFont(font)self.label_5.setMouseTracking(False)self.label_5.setStyleSheet("background-color: rgba(222,222,222,222);")self.label_5.setLineWidth(3)self.label_5.setIndent(5)self.label_5.setObjectName("label_5")self.verticalLayout.addWidget(self.label_5)self.textEdit_3 = QtWidgets.QTextEdit(self.centralwidget)self.textEdit_3.setObjectName("textEdit_3")self.verticalLayout.addWidget(self.textEdit_3)self.verticalLayout.setStretch(0, 1)MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtWidgets.QMenuBar(MainWindow)self.menubar.setGeometry(QtCore.QRect(0, 0, 900, 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")self.menu_4 = QtWidgets.QMenu(self.menubar)self.menu_4.setObjectName("menu_4")self.menu_5 = QtWidgets.QMenu(self.menubar)self.menu_5.setObjectName("menu_5")MainWindow.setMenuBar(self.menubar)self.statusbar = QtWidgets.QStatusBar(MainWindow)self.statusbar.setObjectName("statusbar")MainWindow.setStatusBar(self.statusbar)self.action = QtWidgets.QAction(MainWindow)self.action.setObjectName("action")self.action_2 = QtWidgets.QAction(MainWindow)self.action_2.setObjectName("action_2")self.menu.addAction(self.action)self.menu.addAction(self.action_2)self.menubar.addAction(self.menu.menuAction())self.menubar.addAction(self.menu_2.menuAction())self.menubar.addAction(self.menu_3.menuAction())self.menubar.addAction(self.menu_4.menuAction())self.menubar.addAction(self.menu_5.menuAction())self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))self.pushButton.setText(_translate("MainWindosetIconTextw", "开 始 翻 译"))self.label_5.setText(_translate("MainWindow", "例句/短语"))self.menu.setTitle(_translate("MainWindow", "菜单"))self.menu_2.setTitle(_translate("MainWindow", "工具"))self.menu_3.setTitle(_translate("MainWindow", "设置"))self.menu_4.setTitle(_translate("MainWindow", "视图"))self.menu_5.setTitle(_translate("MainWindow", "帮助"))self.action.setText(_translate("MainWindow", "收藏"))self.action_2.setText(_translate("MainWindow", "添加入词库"))self.action_2.setIconText(_translate("MainWindow", "添加入词库"))self.pushButton.clicked.connect(self.ksfy)#对用户输入的进行判断def ksfy(self):word=self.textEdit.toPlainText()word=word.replace("\n","").replace("?","").replace("!","").replace(" ","%20").replace(",","") #防止输入回车导致报错a,b=pacho(word)if len(a)==0 and len(b)==0:QMessageBox.critical(None,'警告','十分抱歉,未能找到您输入对应的翻译\n请重新输入',QMessageBox.Yes)else:s=''for cy in a:s=s+str(cy)+'\n\n'self.textEdit_2.setText(s)s=''for lj in b:s=s+str(lj)+'\n\n'self.textEdit_3.setText(s)
第二个fy_main.py文件:
from PyQt5.QtWidgets import QApplication,QMainWindow
from Ui_untitled import *
import sysfrom Ui_untitled import Ui_MainWindow
if __name__ == "__main__":app = QApplication(sys.argv)win_fy = QMainWindow()fy = Ui_MainWindow()fy.setupUi(win_fy)win_fy.show()sys.exit(app.exec_())
第三个 :bd_pachong.py
from lxml import etree
from matplotlib.pyplot import flag
import requests
def pacho(word):header={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.33'}page_url='https://dict.youdao.com/w/'+word+'/#keyfrom=dict2.top'page_text=requests.get(url=page_url,headers=header).texttree=etree.HTML(page_text)# tree1 = html.tostring(tree[0])# #编码'utf-8'# tree2 = HTMLParser().unescape(tree1.decode('utf-8'))# print(tree2)res = Truefor w in word: #判断是否全是中文if not '\u4e00' <= w <= '\u9fff':res = Falseif word.encode('utf-8').isalpha(): #判断是否全是英文ciyi=tree.xpath('//*[@id="phrsListTab"]/div[2]/ul/li/text()')liju=tree.xpath('//*[@id="collinsResult"]/div/div/div/div/ul/li/div/div/p/text()')return ciyi,lijuelse: changju=tree.xpath('//*[@id="fanyiToggle"]/div/p[2]/text()')ciyi=tree.xpath('//*[@id="phrsListTab"]/div[2]/ul/p[1]/span/a/text()') liju=tree.xpath('//*[@id="webPhrase"]/p/span/a/text()')dy=tree.xpath('//*[@id="webPhrase"]/p/text()')dy_a=[]for i in dy: #数据清洗if len(i.replace("\n","").replace(" ",""))==0:continue dy_a.append(i.replace("\n","").replace(" ",""))for i in range(len(liju)):liju[i]=liju[i]+':'+dy_a[i] #归并数据if len(liju)==0 and len(ciyi)==0:liju=changjureturn ciyi,lijuif __name__=='__main__':pacho()
二、登录界面:
这的登录界面只需要去看我的另一篇文章,之需要将登录那个板块的代码复制,修改一下素材的路径就可以了,这里就不赘述了。
pyqt5重庆连接二手房数据可视化完整项目_lanzha的博客-CSDN博客
总结
这里只展示了翻译节目的制作,以及爬虫的文件,数据来源与有道。爬虫可以直接作为单个项目是运行。下面是整个完整项目的压缩包
一个可视化界面的翻译项目,可以实现汉译英与英译汉。-Python文档类资源-CSDN文库