Python pyqt4从函数中访问QTextEdit

时间:2015-09-25 13:41:29

标签: python pyqt4 qtextedit

我正在尝试编写一个记事本应用程序,到目前为止,我有一个没有功能的gui。我的gui的每个元素都在单独的函数中,然后在 init 方法中调用。 例如在create_new_file(self)函数中,我试图从QTextEdit .toPlainText()方法获取文本,但是如何从text_edit_area(self)函数访问该字段?

import sys
from PyQt4 import QtGui, QtCore


class Editor(QtGui.QMainWindow):

    def __init__(self):
        super(Editor, self).__init__()
        self.setGeometry(100, 100, 500, 500)
        self.setWindowTitle('Text Editor')
        self.setWindowIcon(QtGui.QIcon('editor.png'))

        self.statusBar()
        self.main_menu()
        self.text_edit_area()
        self.toolbar()

        self.show()


    def main_menu(self):

        # CREATE MAIN MENU
        menu = self.menuBar()

        # MENU ACTIONS
        file_exit_action = QtGui.QAction('&Exit', self)
        file_exit_action.setShortcut('Ctrl+Q')
        file_exit_action.setStatusTip('Close application')
        file_exit_action.triggered.connect(self.close_application)

        file_new_file_action = QtGui.QAction('&New File', self)
        file_new_file_action.setShortcut('Ctrl+N')
        file_new_file_action.setStatusTip('Create a new file')
        file_new_file_action.triggered.connect(self.create_new_file)

        file_open_file_action = QtGui.QAction('&Open File', self)
        file_open_file_action.setShortcut('Ctrl+O')
        file_open_file_action.setStatusTip('Open file')
        file_open_file_action.triggered.connect(self.open_file)

        file_save_file_action = QtGui.QAction('&Save File', self)
        file_save_file_action.setShortcut('Ctrl+S')
        file_save_file_action.setStatusTip('Save opened file')
        file_save_file_action.triggered.connect(self.save_file)

        edit_undo_action = QtGui.QAction('&Undo', self)
        edit_undo_action.triggered.connect(self.undo)

        format_change_font_action = QtGui.QAction('&Change Font', self)
        format_change_font_action.triggered.connect(self.change_font)

        view_maximize_action = QtGui.QAction('&Maximize', self)
        view_maximize_action.triggered.connect(self.maximize)

        help_about_action = QtGui.QAction('&About', self)
        help_about_action.triggered.connect(self.about)


        # FILE MENU
        file_menu = menu.addMenu('&File')
        file_menu.addAction(file_exit_action)
        file_menu.addAction(file_new_file_action)
        file_menu.addAction(file_open_file_action)
        file_menu.addAction(file_save_file_action)


        # EDIT MENU
        edit_menu = menu.addMenu('&Edit')
        edit_menu.addAction(edit_undo_action)

        # FORMAT MENU
        format_menu = menu.addMenu('&Format')
        format_menu.addAction(format_change_font_action)

        # VIEW MENU
        view_menu = menu.addMenu('&View')
        view_menu.addAction(view_maximize_action)

        # HELP MENU
        help_menu = menu.addMenu('&Help')
        help_menu.addAction(help_about_action)


    def toolbar(self):

        # CREATE MAIN TOOLBAR
        tool_bar = self.addToolBar('main toolbar')


        # TOOLBAR ACTION
        toolbar_new_file_action = QtGui.QAction(QtGui.QIcon('new_file.png'),
                                                '&New File', self)
        toolbar_new_file_action.triggered.connect(self.create_new_file)

        toolbar_open_file_action = QtGui.QAction(QtGui.QIcon('open_file.png'),
                                                 '&Open File', self)
        toolbar_open_file_action.triggered.connect(self.open_file)


        # ADD TOOLBAR ACTIONS
        tool_bar.addAction(toolbar_new_file_action)
        tool_bar.addAction(toolbar_open_file_action)





    def text_edit_area(self):
        text_edit = QtGui.QTextEdit()
        self.setCentralWidget(text_edit)



    def close_application(self):
        choice = QtGui.QMessageBox.question(self,
                                            'Confirmation',
                                            'Do you really want to quit?',
                                            QtGui.QMessageBox.Yes |
                                            QtGui.QMessageBox.No)
        if choice == QtGui.QMessageBox.Yes:
            sys.exit()
        else:
            pass


    def create_new_file(self):
        print('create new file')


    def open_file(self):
        print('open file')


    def save_file(self):
        print('saving file')


    def undo(self):
        print('undo')


    def maximize(self):
        print('maximize')


    def change_font(self):
        print('change font')


    def about(self):
        print('about')


def main():
    app = QtGui.QApplication(sys.argv)
    gui = Editor()
    sys.exit(app.exec_())



if __name__ == '__main__':
    main()

2 个答案:

答案 0 :(得分:1)

您可以创建一个实例属性来存储对QTextEdit的引用

self.text_edit = QtGui.QTextEdit()

或者通过调用QMainWindow的centralWidget()方法

来检索引用

答案 1 :(得分:0)

您应该创建自己的自定义QTextEdit。某些类继承自QTextEdit并以您希望的方式个性化。

你可以根据很多很酷的教程。

Here非常棒。一开始就给了我很多帮助。