简体   繁体   English

Python pyqt4 从函数访问 QTextEdit

[英]Python pyqt4 access QTextEdit from function

I'm trying to write a notepad application, so far i have a gui without functionality.我正在尝试编写一个记事本应用程序,到目前为止我有一个没有功能的 gui。 Every element of my gui is in separate function, and then is called in init method.我的 gui 的每个元素都在单独的函数中,然后在init方法中调用。 For example in create_new_file(self) function I was trying to get text from QTextEdit .toPlainText() method, but how can i access this field from text_edit_area(self) function?例如,在 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()

You either create an instance attribute that stores a reference to the QTextEdit您可以创建一个实例属性来存储对 QTextEdit 的引用

self.text_edit = QtGui.QTextEdit()

or you retrieve a reference by calling the centralWidget() method of QMainWindow或者您通过调用 QMainWindow 的centralWidget()方法检索引用

You should create your own custom QTextEdit.您应该创建自己的自定义 QTextEdit。 Some class that inherit from QTextEdit and personalize the way you want.一些继承自 QTextEdit 并以您想要的方式个性化的类。

There are many cool tutorials you could go based on.您可以参考许多很酷的教程。

Here is a really amazing one. 是一个非常惊人的。 Helped me a lot in the very beginning.一开始对我帮助很大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM