本篇文章为大家展示了使用PyQt5怎么实现简易电子词典,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
第一个为主程序代码,主要实现槽函数功能。
from PyQt5.QtCore import pyqtSlot from PyQt5.QtWidgets import QMainWindow from PyQt5 import QtWidgets from Ui_E_Dict_Main import Ui_E_Dictory from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * import time, re # 槽函数代码,运行程序需要运行本文件 class MainWindow(QMainWindow, Ui_E_Dictory): """ Class documentation goes here. """ def __init__(self, parent=None): """ Constructor @param parent reference to the parent widget @type QWidget """ super(MainWindow, self).__init__(parent) self.setupUi(self) # 启动时休眠1秒 time.sleep(1) # 按钮1 查找单词,把单词显示在textBrowser的同时,插入历史记录 @pyqtSlot() def on_pushButton_clicked(self): """ Slot documentation goes here. """ # 单词查询,需要先有一个'dict.txt'文件,其中有大量的英文单词和注释 # 此处也可以先把'dict.txt'插入数据库,历史记录和单词本的插入和查询都可以直接操作数据库 # 不过数据库需要事先安装数据库,并建立相应的表,不好打包,不太方便 word=self.lineEdit.text() f=open('dict.txt', 'r') for line in f: # 对字典文件的数据进行分析,拆解为适合显示的格式 l = re.split('[ ]+',line) if l[0]==word: interpret=' '.join(l[1:]) data='%s\n %s'%(l[0], interpret) # interpret='%s: %s'%(l[0],' '.join(l[1:])) self.textBrowser.setText(data) # 当地时间 t1=time.localtime() t2=time.asctime(t1) #self.lineEdit.setText("")#lineEdit输入后清零,可要可不要 try: # 把所查询单词插入历史记录中, #'a'以只写文件打开一个文件,如果有原文件则追加到文件末尾 file=open('history.txt', 'at') msg='%s %s'%(word, t2) file.write(msg) file.write('\n') file.close() except Exception as e: print(e) f.close() @pyqtSlot() def on_pushButton_2_clicked(self): """ Slot documentation goes here. """ try: # 查询历史记录,把历史记录显示在textBrowser中 file=open('history.txt', 'rt') list=file.readlines() msg=''.join(list) self.textBrowser.setText(msg) file.close() except Exception as e: print(e) @pyqtSlot() def on_pushButton_3_clicked(self): """ Slot documentation goes here. """ try: # 查询单词本,把单词本显示在textBrowser中 file=open('words.txt', 'rt') list=file.readlines() msg=''.join(list) self.textBrowser.setText(msg) file.close() except Exception as e: print(e) @pyqtSlot() def on_pushButton_4_clicked(self): """ Slot documentation goes here. """ word=self.lineEdit.text() try: # 把所查询单词插入单词本中 file=open('words.txt', 'at') file.write(word) file.write('\n') file.close() except Exception as e: print(e) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) # 启动界面的实现,可以使程序更加炫酷 splash=QtWidgets.QSplashScreen(QPixmap("Saved Pictures/5b517f520feaa.jpg")) # 启动界面显示 splash.show() # 在启动界面中显示程序加载进度,参数意思分别为居中显示,蓝色字体 splash.showMessage('正在加载图片资源', Qt.AlignCenter, Qt.blue) time.sleep(1) # 为了不与主程序干扰 app.processEvents() ui = MainWindow() ui.show() # 启动界面完成 splash.finish(ui) sys.exit(app.exec_())
第二个程序代码,主要实现整体的GUI界面的构建(使用Qtdesiner可以极大的简化工作量,强烈推荐)
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_E_Dictory(object): def setupUi(self, E_Dictory): E_Dictory.setObjectName("E_Dictory") E_Dictory.resize(658, 474) icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap("icon/24-monitor.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) E_Dictory.setWindowIcon(icon) self.centralWidget = QtWidgets.QWidget(E_Dictory) self.centralWidget.setObjectName("centralWidget") self.pushButton = QtWidgets.QPushButton(self.centralWidget) self.pushButton.setGeometry(QtCore.QRect(390, 400, 91, 31)) font = QtGui.QFont() font.setFamily("黑体") font.setPointSize(14) self.pushButton.setFont(font) self.pushButton.setObjectName("pushButton") self.label_2 = QtWidgets.QLabel(self.centralWidget) self.label_2.setGeometry(QtCore.QRect(100, 400, 51, 31)) font = QtGui.QFont() font.setFamily("黑体") font.setPointSize(14) self.label_2.setFont(font) self.label_2.setObjectName("label_2") self.textBrowser = QtWidgets.QTextBrowser(self.centralWidget) self.textBrowser.setGeometry(QtCore.QRect(100, 110, 381, 271)) self.textBrowser.setStyleSheet("background-color: rgb(242, 255, 233);") self.textBrowser.setObjectName("textBrowser") self.lineEdit = QtWidgets.QLineEdit(self.centralWidget) self.lineEdit.setGeometry(QtCore.QRect(160, 400, 211, 31)) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(10) self.lineEdit.setFont(font) self.lineEdit.setText("") self.lineEdit.setObjectName("lineEdit") self.label = QtWidgets.QLabel(self.centralWidget) self.label.setGeometry(QtCore.QRect(240, 60, 151, 31)) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(14) font.setBold(False) font.setWeight(50) self.label.setFont(font) self.label.setObjectName("label") self.pushButton_2 = QtWidgets.QPushButton(self.centralWidget) self.pushButton_2.setGeometry(QtCore.QRect(510, 140, 75, 23)) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_3 = QtWidgets.QPushButton(self.centralWidget) self.pushButton_3.setGeometry(QtCore.QRect(510, 220, 75, 23)) self.pushButton_3.setObjectName("pushButton_3") self.pushButton_4 = QtWidgets.QPushButton(self.centralWidget) self.pushButton_4.setGeometry(QtCore.QRect(510, 310, 75, 23)) self.pushButton_4.setObjectName("pushButton_4") self.graphicsView = QtWidgets.QGraphicsView(self.centralWidget) self.graphicsView.setGeometry(QtCore.QRect(0, 0, 661, 471)) self.graphicsView.setStyleSheet("border-image: url(:/pic/Saved Pictures/f3cb924702022fc35eb6f865d67e23a6.jpg);") self.graphicsView.setObjectName("graphicsView") self.graphicsView.raise_() self.pushButton.raise_() self.label_2.raise_() self.textBrowser.raise_() self.lineEdit.raise_() self.label.raise_() self.pushButton_2.raise_() self.pushButton_3.raise_() self.pushButton_4.raise_() E_Dictory.setCentralWidget(self.centralWidget) self.retranslateUi(E_Dictory) QtCore.QMetaObject.connectSlotsByName(E_Dictory) def retranslateUi(self, E_Dictory): _translate = QtCore.QCoreApplication.translate E_Dictory.setWindowTitle(_translate("E_Dictory", "无道词典")) self.pushButton.setText(_translate("E_Dictory", "查找")) # 快捷键回车,可以使查找按钮发生效果 self.pushButton.setShortcut(_translate("E_Dictory", "Return")) self.label_2.setText(_translate("E_Dictory", "单词:")) # setHtml样式表可以按照自己喜好修改 self.textBrowser.setHtml(_translate("E_Dictory", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n" "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n" "p, li { white-space: pre-wrap; }\n" "</style></head><body style=\" font-family:\'SimSun\'; font-size:9pt; font-weight:400; font-style:normal;\">\n" "<p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>")) self.label.setText(_translate("E_Dictory", "查询单词")) self.pushButton_2.setText(_translate("E_Dictory", "历史记录")) self.pushButton_3.setText(_translate("E_Dictory", "单词本")) self.pushButton_4.setText(_translate("E_Dictory", "添加单词")) import dict_rc if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) E_Dictory = QtWidgets.QMainWindow() ui = Ui_E_Dictory() ui.setupUi(E_Dictory) E_Dictory.show() sys.exit(app.exec_())
上述内容就是使用PyQt5怎么实现简易电子词典,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。