可以显示汉字的拼音,支持多音字。
首先用pyqt5拖一个界面出来,然后pyuic转成py文件。[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'jiemian.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(302, 223)
font = QtGui.QFont()
font.setFamily("Adobe Heiti Std")
font.setPointSize(14)
MainWindow.setFont(font)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(24, 60, 75, 31))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(110, 20, 161, 31))
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(14)
self.lineEdit.setFont(font)
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(24, 24, 91, 21))
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(14)
self.label.setFont(font)
self.label.setObjectName("label")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(110, 60, 161, 141))
font = QtGui.QFont()
font.setFamily("宋体")
font.setPointSize(14)
self.listWidget.setFont(font)
self.listWidget.setObjectName("listWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "拼音查询"))
self.pushButton.setText(_translate("MainWindow", "查询"))
self.label.setText(_translate("MainWindow", "粘贴输入"))
然后去写功能代码,引入界面class,不在ui文件中写逻辑。这样更新UI的话,功能代码不必重写。[Python] 纯文本查看 复制代码 import sys
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5 import QtWidgets
from jiemian import Ui_MainWindow
from pypinyin import lazy_pinyin, Style, pinyin
class my(Ui_MainWindow):
def setupUi(self, MainWindow):
super(my, self).setupUi(MainWindow)
self.pushButton.clicked.connect(self.xianshipingyin)
def __init__(self):
pass
def xianshipingyin(self):
self.listWidget.clear()
neirong=self.lineEdit.text()
if len(neirong)==0:
self.listWidget.addItem('输入不能为空')
return
jieguo = pinyin(neirong, heteronym=True)
for i in range(0, len(neirong) ):
duoyin = ''
if len(jieguo[i])>1:
for j in jieguo[i]:
duoyin=duoyin+j+' '
else:
duoyin=jieguo[i][0]
duyin=neirong[i]+":"+ duoyin
self.listWidget.addItem(duyin)
myapp = QApplication(sys.argv) # sys.argv 主程序的名字
mydlg = QtWidgets.QMainWindow()
myui =my()
myui.setupUi(mydlg)
mydlg.show()
sys.exit(myapp.exec_()) 最后pyinstaller -F -w zhuchengxu.py即可
成品下载:(64位windows系统)
https://wwi.lanzout.com/iaQ4v09w6ngh |