Reputation: 161
I'm trying to write a notepad application, so far i have a gui without functionality. Every element of my gui is in separate function, and then is called in init method. 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?
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()
Upvotes: 2
Views: 301
Reputation: 1640
You should create your own custom QTextEdit. Some class that inherit from QTextEdit and personalize the way you want.
There are many cool tutorials you could go based on.
Here is a really amazing one. Helped me a lot in the very beginning.
Upvotes: 0
Reputation: 1039
You either create an instance attribute that stores a reference to the QTextEdit
self.text_edit = QtGui.QTextEdit()
or you retrieve a reference by calling the centralWidget()
method of QMainWindow
Upvotes: 1