QTPY+python背单词软件

QTPY+python背单词软件_第1张图片

# encoding: UTF-8
from qtpy import QtWidgets, QtGui, QtCore
import sys
from collections import OrderedDict
import random


class WordMonitor(QtWidgets.QTableWidget):
    

    # ----------------------------------------------------------------------
    def __init__(self, parent=None):
        """Constructor"""
        super(WordMonitor, self).__init__(parent)

        self.keyCellDict = {}
        self.data = None
        self.inited = False
        self.word_file_list=[]

        self.initUi()

    # ----------------------------------------------------------------------
    def initUi(self):
        """初始化界面"""
        self.setRowCount(1)
        self.verticalHeader().setVisible(False)
        self.setEditTriggers(self.NoEditTriggers)

        self.setMaximumHeight(self.sizeHint().height())


    # ----------------------------------------------------------------------
    def updateData(self, data):
        """更新数据"""
        if not self.inited:
            self.setColumnCount(len(data))
            self.setHorizontalHeaderLabels(data.keys())

            col = 0
            for k, v in data.items():
                cell = QtWidgets.QTableWidgetItem(unicode(v))
                self.keyCellDict[k] = cell
                self.setItem(0, col, cell)
                col += 1

            self.inited = True
        else:
            for k, v in data.items():
                cell = self.keyCellDict[k]
                cell.setText(unicode(v))
        self.resizeColumnsToContents()


    def set_word_file_path(self,word_file_path):

        f = open(word_file_path, 'rb')
        index = 0
        self.word_file_list = []
        while (index < 5000):
            f.seek(290 + 128 * index, 0)
            s1 = f.read(30)
            if s1 == "" or len(s1) < 30:
                break
            s1 = s1.strip()
            f.seek(290 + 128 * index + 60, 0)
            s2 = f.read(42).strip()
            if s2.find("    ") != -1:
                s2 = s2[:s2.find("    ")]
            self.word_file_list.append([s1, s2])
            index = index + 1
            # print('%s   %s'%(s1,s2.decode('gbk')))
        # print(word_file_list)
        f.close()

    def updateWord(self):
        if self.word_file_list==[]:
            self.set_word_file_path('123.gds')

        word=random.sample(self.word_file_list,1)

        other=random.sample(self.word_file_list,3)

        # print(word[0][0],word[0][1])



        dict = OrderedDict()
        dict['Word'] = word[0][0]
        dict['1'] = word[0][1].decode('gbk')
        dict['2'] = other[0][1].decode('gbk')
        dict['3'] = other[2][1].decode('gbk')
        dict['4'] = other[1][1].decode('gbk')

        self.updateData(dict)


class WordManager(QtWidgets.QGroupBox):
    


    # ----------------------------------------------------------------------
    def __init__(self, parent=None):
        """Constructor"""
        super(WordManager, self).__init__(parent)

        self.initUi()

        self.setMinimumHeight(150)

    # ----------------------------------------------------------------------
    def initUi(self):
        """初始化界面"""
        self.setTitle('word')

        self.Monitor = WordMonitor(self)
        self.Monitor.updateWord()


        buttonInit = QtWidgets.QPushButton('Next')
        buttonInit.clicked.connect(self.nextword)

        # buttonInit.clicked.connect(self.init)
        hbox1 = QtWidgets.QHBoxLayout()
        hbox1.addWidget(self.Monitor)
        hbox1.addWidget(buttonInit)

        vbox = QtWidgets.QVBoxLayout()
        vbox.addLayout(hbox1)



        self.setLayout(vbox)

    def nextword(self):
        self.Monitor.updateWord()



def main():
    qApp = QtWidgets.QApplication([])



    test=WordManager()


    test.show()
    sys.exit(qApp.exec_())


if __name__ == '__main__':
    main()

 

 

你可能感兴趣的:(python)