学写字小工具
本帖最后由 saint80 于 2023-4-12 14:23 编辑先上图
import sys
import requests
import os
from PySide2 import QtWidgets, QtGui, QtCore
from PySide2.QtCore import Qt
from bs4 import BeautifulSoup
class HanziSearch(QtWidgets.QWidget):
def __init__(self):
super(HanziSearch, self).__init__()
self.setWindowTitle('静待花开久不开')
self.layout_up = QtWidgets.QHBoxLayout()
self.layout_down = QtWidgets.QHBoxLayout()
self.edit_find = QtWidgets.QLineEdit()
self.edit_find.setFixedHeight(50)
self.edit_find.setPlaceholderText("请输入一个汉字")
self.btn_search = QtWidgets.QPushButton("查询")
self.layout_up.addWidget(self.edit_find)
self.layout_up.addWidget(self.btn_search)
self.lbl_gif = QtWidgets.QLabel()
self.movie = QtGui.QMovie('./空.gif')
self.lbl_gif.setMovie(self.movie)
self.movie.start()
self.lbl_gif.setWindowFlags(Qt.SplashScreen)
self.layout_right = QtWidgets.QVBoxLayout()
self.edit_pinyin = QtWidgets.QLineEdit()
self.edit_pinyin.setFixedHeight(50)
self.edit_pinyin.setPlaceholderText("拼音")
self.edit_radical = QtWidgets.QLineEdit()
self.edit_radical.setFixedHeight(50)
self.edit_radical.setPlaceholderText("部首")
self.edit_stroke = QtWidgets.QLineEdit()
self.edit_stroke.setFixedHeight(50)
self.edit_stroke.setPlaceholderText("笔画")
self.edit_traditional = QtWidgets.QLineEdit()
self.edit_traditional.setFixedHeight(50)
self.edit_traditional.setPlaceholderText("繁体")
self.edit_wubi = QtWidgets.QLineEdit()
self.edit_wubi.setFixedHeight(50)
self.edit_wubi.setPlaceholderText("五笔")
self.layout_right.addWidget(self.edit_pinyin)
self.layout_right.addWidget(self.edit_radical)
self.layout_right.addWidget(self.edit_stroke)
self.layout_right.addWidget(self.edit_traditional)
self.layout_right.addWidget(self.edit_wubi)
self.layout_down.addWidget(self.lbl_gif)
self.layout_down.addLayout(self.layout_right)
self.setLayout(QtWidgets.QVBoxLayout())
self.layout().addLayout(self.layout_up)
self.layout().addLayout(self.layout_down)
self.btn_search.clicked.connect(self.search_hanzi)
#界面美化
self.edit_find.setFont(QtGui.QFont("微软雅黑", 18))
self.btn_search.setFont(QtGui.QFont("微软雅黑", 18))
self.btn_search.setStyleSheet("background-color:#37a7e8; color:#fff")
self.edit_pinyin.setFont(QtGui.QFont("微软雅黑", 18))
self.edit_radical.setFont(QtGui.QFont("微软雅黑", 18))
self.edit_stroke.setFont(QtGui.QFont("微软雅黑", 18))
self.edit_traditional.setFont(QtGui.QFont("微软雅黑", 18))
self.edit_wubi.setFont(QtGui.QFont("微软雅黑", 18))
self.layout_up.setSpacing(20)
self.layout_down.setSpacing(60)
self.layout_right.setSpacing(18)
self.layout_right.setContentsMargins(0, 0, 0, 0)
self.setStyleSheet(
'''
QWidget{background-color:#fff;}
QGridLayout::item{margin:5px;}
QLineEdit{border:1px solid #d3d3d3;border-radius: 5px;height:24px;}
QLineEdit:hover{border:1px solid #979797;}
QPushButton{border:1px solid #37a7e8;border-radius:2px;color:#fff;padding: 4px 16px;background:#37a7e8;}
QPushButton:hover{background-color:#4fbcff;}
'''
)
def search_hanzi(self):
self.btn_search.setText("查询中...")
self.btn_search.setEnabled(False)
hanzi = self.edit_find.text()
if not self.is_chinese(hanzi) or hanzi=='' or len(hanzi)>1:
QtWidgets.QMessageBox.warning(self, "提示", "请输入一个汉字!")
self.btn_search.setText("查询")
self.btn_search.setEnabled(True)
else:
self.movie.start()
self.finder(hanzi)
def finder(self, hanzi):
self.movie.stop()
filename = f"{hanzi}.gif"
url = f"https://hanyu.baidu.com/zici/s?wd={hanzi}"
html = requests.get(url)
soup = BeautifulSoup(html.text, features="html.parser")
if os.path.isfile(filename):
self.movie.setFileName(filename)
self.movie.start()
else:
img = soup.findAll("img", {"class": "bishun"}).get("data-gif")
res = requests.get(img)
with open(filename,"wb") as f:
f.write(res.content)
self.movie.setFileName(filename)
self.movie.start()
#拼音
pinyin = soup.findAll("div", {"id": "pinyin"}).find_all("b").string
self.edit_pinyin.setText('拼音:' + pinyin)
#部首
radical = soup.findAll("li", {"id": "radical"}).find_all("span").string
self.edit_radical.setText('部首:' + radical)
#笔数
stroke = soup.findAll("li", {"id": "stroke_count"}).find_all("span").string
self.edit_stroke.setText('笔数:' + stroke)
#繁体
traditional = soup.findAll("li", {"id": "traditional"}).find_all("span").string
self.edit_traditional.setText('繁体:' + traditional)
#五笔
wubi = soup.findAll("li", {"id": "wubi"}).find_all("span").string
self.edit_wubi.setText('五笔:' + wubi)
self.btn_search.setText("查询")
self.btn_search.setEnabled(True)
def is_chinese(self, s):
for c in s:
if not ('\u4e00' <= c <= '\u9fff'):
return False
return True
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = HanziSearch()
window.resize(850, 600)
window.show()
sys.exit(app.exec_())
我打包了下。
链接:https://pan.baidu.com/s/1cFqBaMDgPoRWJBhS-pbhyg
提取码:17qu 想问一下自学python能学会吗,本人快40岁了,正准备找找视频教程 哎,人家学爬虫爬小姐姐,我只能为儿子爬百度汉字。包括笔顺、部首、拼音只取了第一个,以后再修改!
一个静待花开却久久不开的可怜爸爸的无聊东东! 没图 差评 哈哈 无图无真相 saint80 发表于 2023-4-12 14:04
哎,人家学爬虫爬小姐姐,我只能为儿子爬百度汉字。包括笔顺、部首、拼音只取了第一个,以后再修改!
一个 ...
感谢兄台的意见! liuhaigang12 发表于 2023-4-12 14:17
没图 差评 哈哈
感谢兄台的意见! 建议直接打包的exe
放到我的pycharm中 运行不了。 期待着打包的exe出现 期待成品 ModuleNotFoundError: No module named 'requests'
小小白一个,什么原因?