用QT写了个桌面字典,分享点思路,大神绕道
最近在实验楼参加了一个训练营,其中第三个项目就是利用有道翻译接口开发一个可视化划词翻译的小程序,但实验楼是运用Pygtk框架开发的,我用惯了拖控件的开发形式,一时之间还是没法接受,纯手写UI,感觉思路好乱,于是乎我决定使用QT作为UI的展示.....这仅仅是一个小小的部分,没啥技术可言,所以大牛还请绕道!实验目标:通过爬取有道翻译接口,进行交互,并实现划词翻译功能 完成进度:目前已实现大部分功能,通过模拟浏览器取得翻译结果,并通过基于QT框架展示的效果! 由于刚接触QT,难免会碰到歪路,目前的展示功能有:1,翻译结果 2.百科 3.词条功能
以下是爬虫代码部分:
#coding:utf-8
import requests,urllib
from bs4 import BeautifulSoup
def request(arg):
youdao_url = 'http://www.youdao.com/w/%s/#keyfrom=dict2.top'% arg
Referer = 'http://www.youdao.com/w/%s/'%urllib.parse.quote(arg)
print(Referer)
herders = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate, sdch',
'Connection':'keep-alive',
'Host':'www.youdao.com',
'Referer':Referer,
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36',
}
session = requests.session()
get_youdao = session.get(youdao_url,headers = herders)
soup = BeautifulSoup(get_youdao.text,'lxml')
jieguo = []
baike = []
youmei = []
jieguo_temp = ''
baike_temp = ''
youmei_temp = ''
for i in soup.select('#results-contents'):
#print(i.select('.trans-container').text.strip('\n').split())
#print(i.select('#webTrans').text)
#print(i.select('#examples').text)
try:
jieguo.append(i.select('.trans-container').text)
baike.append(i.select('#webTrans').text)
youmei.append( i.select('#examples').text)
except IndexError:
baike.append(['噢恼,该词汇或句子未被本百科收录!','抱歉啦,当前词条搜索为空。'])
youmei.append(['噢恼,该词汇或句子未被本百科收录!', '抱歉啦,当前词条搜索为空。'])
for i in jieguo:
jieguo_temp = ''.join(i)
for j in baike:
baike_temp = ''.join(j)
for x in youmei:
youmei_temp = ''.join(x)
print(jieguo)
return jieguo_temp,baike_temp,youmei_temp
以下是UI代码 - 基于QT设计器生成:
from PyQt5 import QtCore, QtGui, QtWidgets
from translation import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1115, 875)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(1040, 30, 71, 31))
self.pushButton.setObjectName("pushButton")
self.lineEdit_shuru = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_shuru.setGeometry(QtCore.QRect(10, 30, 1021, 31))
self.lineEdit_shuru.setObjectName("lineEdit_shuru")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 10, 1101, 16))
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(14)
self.label.setFont(font)
self.label.setAcceptDrops(False)
self.label.setLayoutDirection(QtCore.Qt.RightToLeft)
self.label.setTextFormat(QtCore.Qt.PlainText)
self.label.setObjectName("label")
self.textEdit_baike = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit_baike.setGeometry(QtCore.QRect(10, 260, 1101, 241))
self.textEdit_baike.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.textEdit_baike.setReadOnly(True)
self.textEdit_baike.setObjectName("textEdit_baike")
self.lineEdit_jieguo = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_jieguo.setGeometry(QtCore.QRect(10, 100, 1101, 111))
self.lineEdit_jieguo.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.lineEdit_jieguo.setText("")
self.lineEdit_jieguo.setEchoMode(QtWidgets.QLineEdit.Normal)
self.lineEdit_jieguo.setCursorPosition(0)
self.lineEdit_jieguo.setReadOnly(True)
self.lineEdit_jieguo.setObjectName("lineEdit_jieguo")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(10, 70, 1101, 16))
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(14)
self.label_2.setFont(font)
self.label_2.setAcceptDrops(False)
self.label_2.setLayoutDirection(QtCore.Qt.RightToLeft)
self.label_2.setTextFormat(QtCore.Qt.PlainText)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(10, 230, 1101, 16))
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(14)
self.label_3.setFont(font)
self.label_3.setAcceptDrops(False)
self.label_3.setLayoutDirection(QtCore.Qt.RightToLeft)
self.label_3.setTextFormat(QtCore.Qt.PlainText)
self.label_3.setObjectName("label_3")
self.textEdit_youmei = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit_youmei.setGeometry(QtCore.QRect(10, 550, 1101, 241))
self.textEdit_youmei.viewport().setProperty("cursor", QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.textEdit_youmei.setReadOnly(True)
self.textEdit_youmei.setObjectName("textEdit_youmei")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(10, 520, 1101, 16))
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(14)
self.label_4.setFont(font)
self.label_4.setAcceptDrops(False)
self.label_4.setLayoutDirection(QtCore.Qt.RightToLeft)
self.label_4.setTextFormat(QtCore.Qt.PlainText)
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(510, 800, 161, 16))
self.label_5.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
self.label_5.setObjectName("label_5")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1115, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.translation)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def translation(self):
self.textEdit_baike.clear()
self.textEdit_youmei.clear()
self.lineEdit_jieguo.clear()
self.soup = self.lineEdit_shuru.text()
get_soup = request(self.soup)
jieguo,baike,youmei = get_soup
self.lineEdit_jieguo.setText(jieguo)
self.textEdit_baike.append(baike)
self.textEdit_youmei.append(youmei)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Easy dictionary"))
self.pushButton.setText(_translate("MainWindow", "翻译"))
self.label.setText(_translate("MainWindow", " 欢迎使用简易在线桌面词典,划词翻译更轻松!"))
self.label_2.setText(_translate("MainWindow", "机器翻译结果:"))
self.label_3.setText(_translate("MainWindow", "百科科普:"))
self.label_4.setText(_translate("MainWindow", "优美词条:"))
self.label_5.setText(_translate("MainWindow", "BY:NarutoLee"))
调用窗口代码:
#coding:utf-8
import sys
import fanyi
from PyQt5.QtWidgets import QApplication,QMainWindow
if __name__=="__main__":
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = fanyi.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
最后,我想说....关于QT方面的资料真的是太少了,不知这里有没有学习QT的朋友,如果您有好的学习资料,求共享,哈哈!
http://bbs.fishc.com/forum.php?mod=collection&action=view&ctid=153 介哥PYQT淘贴。
http://bbs.fishc.com/forum.php?mod=collection&action=view&ctid=447 不二QT淘贴。 版主是大神,表示没学UI 表示 只能膜拜
页:
[1]