PyQt 5プロジェクト実習:簡易電子辞書

9581 ワード

PyQt 5はpythonの中で非常に実用的なGUIプログラミングモジュールであり、機能が非常に強い.Pyqtのプログラミングを習ったばかりで、待ちきれずに電子辞書GUIプログラムを書きました.プログラム全体はqt Desinerを使ってguiインタフェース全体をよくし、スロット関数は自分で書いた.電子辞書は単語を検索し、履歴を検索し、単語帳をコレクションし、表示する機能を実現し、プログラムをよりクールにするために、起動インタフェースを追加した.具体的なコードは以下の通りです.
最初のメインプログラムコードは、主にスロット関数機能を実現します.
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
%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('
') 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('
') 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_())

2番目のプログラムコードは、主に全体的な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", ""-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">
" "
" "


")) 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の内容はhtml形式で表示でき、その中のすべてのファイルの表示は自分の好みで設計することができます.