pyqt5 写的 复制翻译器
苦逼于每日看英文文章很多单词不会,然后用有道词典划词翻译感觉总是失灵,自己手写一个基于pyqt5的复制翻译器,可以选择翻译框显示出来的时间长短,选中单词复制然后结果就会弹出来,效果展示见B站
苦逼于看英文要查单词,写了一个可以选择翻译框现实时间的,能够托盘化的复制翻译器,选中内容复制(用鼠标或者ctrl c都可以)然后翻译框跟随现实的应用,pyqt5
原理就是用pyqt5读取粘贴板,然后向金山词霸发送post,然后用beautifulsoup扒开界面上结果所在的标签,然后跟随鼠标位置显示一个Qwidget,输出到QTextBrowser中,父窗口用QTimer限制子窗口的hide和show时间
beautifulsoup提取翻译
def get(self, word_str):
self.word = Word()
self.word.text = word_str
r = self.s.get("http://www.iciba.com/%s" % word_str)
soup = BeautifulSoup(r.content, "lxml")
temp_results = soup.find_all("div", class_="FoldBox_fold__1GZ_2")
if not temp_results:
return True
base = temp_results[0]
#print(base)
# 获取基本词义
temp_results = base.find_all('ul', class_='Mean_part__1RA2V')
if temp_results:
#print(temp_results)
temp = temp_results[0]
for node in temp:
if len(node.contents) != 2:
self.word.props[''] = node.text
else:
temp_prop = node.contents[0].text
#print(temp_prop)
temp_str = node.contents[1].text
self.word.props[temp_prop] = temp_str
# 获取句子翻译
temp_results_sentence = base.find_all(class_='Mean_trans__166Ns')
if temp_results_sentence:
base=temp_results_sentence[0]
temp_p=base.find_all('p')
#print(temp_p[0].text)
self.word.props['']=temp_p[0].text
更新Qwidget
# 设置位置和大小
self.setFixedSize(self.width, self.height)
cur = QtGui.QCursor.pos()
self.x = cur.x() -self.baseB
self.y = cur.y() -self.baseB
# 如果超出了屏幕边界,便显示在里面
window_h = QDesktopWidget().screenGeometry().height()
window_w = QDesktopWidget().screenGeometry().width()
if self.x + self.width > window_w :
self.x=window_w-self.width
if self.y + self.height > window_h:
self.y=window_h-self.height
if self.x + self.height > window_h and self.y + self.width > window_w:
# self.x -= self.baseB + self.width
# self.y -= self.baseB + self.height
self.x=window_w-self.width
self.y=window_h-self.height
self.move(self.x, self.y)
self.show()
if self.timerStart==True:
print(self.baseTime*1000)
#计时自动关闭窗口
self.timer.timeout.connect(self.hide) # 每次计时到时间时发出信号
self.timer.start(self.baseTime*1000) # 设置计时间隔并启动;单位毫秒
base_info = ''
for x in self.translater.word.props:
base_info += x + self.translater.word.props[x] + '\n\n'
#print(base_info)
if base_info is '':
self.text_browser.append("翻译:" + '\n\n' + "无结果")
else:
self.text_browser.append("翻译:"+'\n\n'+base_info)