PyQt5實(shí)現(xiàn)簡(jiǎn)易電子詞典
PyQt5是python中一個(gè)非常實(shí)用的GUI編程模塊,功能十分強(qiáng)大。剛剛學(xué)完了Pyqt的編程,就迫不及待的寫出了一個(gè)電子詞典GUI程序。整個(gè)程序使用qt Desiner把整個(gè)gui界面做好,槽函數(shù)則自己寫好的。電子詞典實(shí)現(xiàn)了查詢單詞,查詢歷史記錄,收藏和查看單詞本的功能,另外為了是程序更加炫酷,還添加了一個(gè)啟動(dòng)界面。具體代碼如下:
第一個(gè)為主程序代碼,主要實(shí)現(xiàn)槽函數(shù)功能。
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 # 槽函數(shù)代碼,運(yùn)行程序需要運(yùn)行本文件 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) # 啟動(dòng)時(shí)休眠1秒 time.sleep(1) # 按鈕1 查找單詞,把單詞顯示在textBrowser的同時(shí),插入歷史記錄 @pyqtSlot() def on_pushButton_clicked(self): """ Slot documentation goes here. """ # 單詞查詢,需要先有一個(gè)'dict.txt'文件,其中有大量的英文單詞和注釋 # 此處也可以先把'dict.txt'插入數(shù)據(jù)庫(kù),歷史記錄和單詞本的插入和查詢都可以直接操作數(shù)據(jù)庫(kù) # 不過數(shù)據(jù)庫(kù)需要事先安裝數(shù)據(jù)庫(kù),并建立相應(yīng)的表,不好打包,不太方便 word=self.lineEdit.text() f=open('dict.txt', 'r') for line in f: # 對(duì)字典文件的數(shù)據(jù)進(jìn)行分析,拆解為適合顯示的格式 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) # 當(dāng)?shù)貢r(shí)間 t1=time.localtime() t2=time.asctime(t1) #self.lineEdit.setText("")#lineEdit輸入后清零,可要可不要 try: # 把所查詢單詞插入歷史記錄中, #'a'以只寫文件打開一個(gè)文件,如果有原文件則追加到文件末尾 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) # 啟動(dòng)界面的實(shí)現(xiàn),可以使程序更加炫酷 splash=QtWidgets.QSplashScreen(QPixmap("Saved Pictures/5b517f520feaa.jpg")) # 啟動(dòng)界面顯示 splash.show() # 在啟動(dòng)界面中顯示程序加載進(jìn)度,參數(shù)意思分別為居中顯示,藍(lán)色字體 splash.showMessage('正在加載圖片資源', Qt.AlignCenter, Qt.blue) time.sleep(1) # 為了不與主程序干擾 app.processEvents() ui = MainWindow() ui.show() # 啟動(dòng)界面完成 splash.finish(ui) sys.exit(app.exec_())
第二個(gè)程序代碼,主要實(shí)現(xiàn)整體的GUI界面的構(gòu)建(使用Qtdesiner可以極大的簡(jiǎn)化工作量,強(qiáng)烈推薦)
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", "查找")) # 快捷鍵回車,可以使查找按鈕發(fā)生效果 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_())
textBrowser中的內(nèi)容可以以html的格式顯示出來,所有其中的文件的顯示可以按照自己的喜好來設(shè)計(jì)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mac下pycharm設(shè)置python版本的圖文教程
今天小編就為大家分享一篇mac下pycharm設(shè)置python版本的圖文教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06基于Python數(shù)據(jù)可視化利器Matplotlib,繪圖入門篇,Pyplot詳解
下面小編就為大家?guī)硪黄赑ython數(shù)據(jù)可視化利器Matplotlib,繪圖入門篇,Pyplot詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10python?包中的sched?事件調(diào)度器的操作方法
sched模塊內(nèi)容很簡(jiǎn)單,只定義了一個(gè)類。它用來最為一個(gè)通用的事件調(diào)度模塊,接下來通過本文給大家介紹python?包之?sched?事件調(diào)度器教程,需要的朋友可以參考下2022-04-04基于Python的接口自動(dòng)化讀寫excel文件的方法
這篇文章主要介紹了基于Python的接口自動(dòng)化讀寫excel文件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01讓Python腳本暫停執(zhí)行的幾種方法(小結(jié))
這篇文章主要介紹了讓Python腳本暫停執(zhí)行的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Python 實(shí)現(xiàn)打印單詞的菱形字符圖案
這篇文章主要介紹了Python 實(shí)現(xiàn)打印單詞的菱形字符圖案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04一文教會(huì)你用Python實(shí)現(xiàn)pdf轉(zhuǎn)word
python實(shí)現(xiàn)pdf轉(zhuǎn)word,支持中英文轉(zhuǎn)換,轉(zhuǎn)換精度高,可以達(dá)到使用效果,下面這篇文章主要給大家介紹了關(guān)于用Python實(shí)現(xiàn)pdf轉(zhuǎn)word的相關(guān)資料,需要的朋友可以參考下2023-01-01