首页 > 代码库 > pyqt MainWindow记录内容

pyqt MainWindow记录内容

class Texts(QtGui.QMainWindow,Ui_MainWindow):

    def __init__(self,parne=None):

        super(Texts,self).__init__(parne)

        self.setupUi(self)

        self.setWindowTitle(u‘文档或者其他‘)

        self.setWindowIcon((QtGui.QIcon(r‘i/e.jpg‘)))

        self.setFixedSize(500,550)

        self.action_3_clear.setEnabled(False)

        self.showtext.setEnabled(False)

        self.adjustSize()

        import random

        rand1=‘#FFFFFF‘

        rand2=‘#000099‘

        self.showtext.setStyleSheet(""

                "color: %s;"

                "margin: 10px;"

                "padding: 5px;"

                "padding-left: 15px;"

                "padding-right: 15px;"

                "border-radius: 5px;"

                "border: 3px solid #abc;"%(rand1,rand2))

        self.showtext.setReadOnly(True)

        self.showtext.setEnabled(True)

        self.setWindowIcon(QtGui.QIcon(r‘i/e.png‘))

        self.action_3_about.triggered.connect(self.aboutn)

        self.action_close.triggered.connect(self.close_)

        self.action_file.triggered.connect(self.openflies)

        self.action_3_clear.triggered.connect(self.qingkong_)

        self.showtext.textChanged.connect(self.updatea)

    def aboutn(self):

        QtGui.QMessageBox.information(self,u‘关于‘,u‘这部分是文档或者文本部分,用于查看有关内容的地方‘)

    def close_(self):

        self.close()

    def openflies(self):

        self.showtext.setEnabled(True)

        fileName=QtGui.QFileDialog.getOpenFileName(self)

        if fileName.isEmpty()==False:

            if self.showtext.document().isEmpty():

                self.loadFile(fileName)

    def loadFile(self,fileName):

        file=QtCore.QFile(fileName)

        if file.open(QtCore.QIODevice.ReadOnly|QtCore.QIODevice.Text):

            textStream=QtCore.QTextStream(file)

            while textStream.atEnd()==False:

                self.showtext.appendPlainText(textStream.readLine())

    def qingkong_(self):

        close1=QtGui.QMessageBox(self)

        close1.setWindowTitle(u‘清空选项‘)

        close1.setWindowIcon(QtGui.QIcon(r‘i/a.png‘))

        yes=close1.addButton(u‘清空(&Yes)‘,QtGui.QMessageBox.ActionRole)

        no=close1.addButton(u‘(&No)‘,QtGui.QMessageBox.ActionRole)

        close1.setText(u‘您真的要退出这个工具吗?‘)

        close1.exec_()

        button=close1.clickedButton()

        if button==yes:

            self.showtext.clear()

        else:pass

    def updatea(self):

        self.action_3_clear.setEnabled(self.showtext.toPlainText().isEmpty()==False)

        self.showtext.setEnabled(True)

如图: