In this article iam going to show you How to Create QTextEdit in Pyside2 , so the QTextEdit class provides a widget that is used to edit and display both plain and rich text. QTextEdit works on paragraphs and characters. A paragraph is a formatted string which is word-wrapped to fit into the width of the widget. By default when reading plain text, one newline signifies a paragraph. A document consists of zero or more paragraphs. The words in the paragraph are aligned in accordance with the paragraph’s alignment. Paragraphs are separated by hard line breaks. Each character within a paragraph has its own attributes, for example, font and color.
Also you can check my previous articles on Pyside2 GUI Development.
1: Getting Started With Pyside2 | Qt For Python
2: Pyside2 GUI Creating First Window
3: Pyside2 GUI Creating Window Icon
4: Pyside2 GUI How To Create Icon Modes
5: Pyside2 GUI How To Create Tooltip
6: Pyside2 GUI QPushButton With Signal And Slot
7: Pyside2 GUI Making Center The Window
8: Python GUI How To Create AboutBox
9: Python GUI How to Create Digital Clock in Pyside2
10: How To Create StatusBar In Pyside2
11: Pyside2 Creating QProgressBar
12: Pyside2 Layout Managment with QHBoxLayout
13: Pyside2 GridLayout Example
16: Pyside2 Creating FontComboBox
17: Pyside2 Creating QCompleter
18: Pyside2 GUI Creating Slider
19: Pyside2 Create MenuBar & MenuItems
So now this is the complete source code for How to Create QTextEdit in Pyside2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
from PySide2.QtWidgets import QApplication, QMainWindow, QAction, QTextEdit import sys from PySide2.QtGui import QIcon, QFont class Window(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Simple Notepad Application") self.setGeometry(300,200,1280,600) self.textEdit = QTextEdit(self) self.textEdit.setFont(QFont('Sanserif', 13)) self.setCentralWidget(self.textEdit) self.setIcon() self.create_menu() self.show() def setIcon(self): appIcon = QIcon("icon.png") self.setWindowIcon(appIcon) def create_menu(self): mainMenu = self.menuBar() fileMenu = mainMenu.addMenu('File') viewMenu = mainMenu.addMenu('View') editMenu = mainMenu.addMenu('Edit') fontMenu = mainMenu.addMenu('Font') helpMenu = mainMenu.addMenu('Help') openAction = QAction(QIcon('open.png'), "Open", self) openAction.setShortcut('Ctrl+O') saveAction = QAction(QIcon('save.png'), "Save", self) saveAction.setShortcut('Ctrl+S') exitAction = QAction(QIcon('exit.png'), "Exit", self) exitAction.setShortcut('Ctrl+X') exitAction.triggered.connect(self.exit_app) fileMenu.addAction(openAction) fileMenu.addAction(saveAction) fileMenu.addAction(exitAction) def exit_app(self): self.close() myapp = QApplication(sys.argv) window = Window() myapp.exec_() sys.exit() |
So in the above code we are using some codes from the previous article , Pyside2 Create MenuBar & MenuItems.
These are the imports that we need in this article.
1 2 3 |
from PySide2.QtWidgets import QApplication, QMainWindow, QAction, QTextEdit import sys from PySide2.QtGui import QIcon, QFont |
OK now in here we are going to set our window title, also we need to set the geometry of the window, like x and y position of the window, also width and height of the window .
1 2 |
self.setWindowTitle("Simple Notepad Application") self.setGeometry(300,200,1280,600) |
Also we need to create our QTextEdit in our main class, first we have created the object of QTexEdit, after that we have set the font for our QTextEdit. also you need to set the text edit as central widget for the window.
1 2 3 |
self.textEdit = QTextEdit(self) self.textEdit.setFont(QFont('Sanserif', 13)) self.setCentralWidget(self.textEdit) |
In here we have created menuBar object and we have added some items to the menuBar.
1 2 3 4 5 6 |
mainMenu = self.menuBar() fileMenu = mainMenu.addMenu('File') viewMenu = mainMenu.addMenu('View') editMenu = mainMenu.addMenu('Edit') fontMenu = mainMenu.addMenu('Font') helpMenu = mainMenu.addMenu('Help') |
OK now in here we have added some menu items with the icons and also shortcuts. make sure that you have some icons in your working directory. we are using QAction for this purpose, so in applications many common commands can be invoked via menus, toolbar buttons, and keyboard shortcuts. since the user expects each command to be performed in the same way, regardless of the user interface used, it is useful to represent each command as an action. Actions can be added to menus and toolbars, and will automatically keep them in sync. For example, in a word processor, if the user presses a Bold toolbar button, the Bold menu item will automatically be checked.
1 2 3 4 5 6 7 8 9 |
openAction = QAction(QIcon('open.png'), "Open", self) openAction.setShortcut('Ctrl+O') saveAction = QAction(QIcon('save.png'), "Save", self) saveAction.setShortcut('Ctrl+S') exitAction = QAction(QIcon('exit.png'), "Exit", self) exitAction.setShortcut('Ctrl+X') |
In here we are going to connect triggered signal of menu item with the slot or method that we will create.
1 |
exitAction.triggered.connect(self.exit_app) |
Now after creation of menu actions, we need to add that to the related menubar like this. basically in this example we want to add the menu items in our file menu.
1 2 3 |
fileMenu.addAction(openAction) fileMenu.addAction(saveAction) fileMenu.addAction(exitAction) |
And this is the method that we have already connected with triggered signal of menu item. this method is just for closing of the window.
1 2 |
def exit_app(self): self.close() |
Also every Pyside2 application must create an application object. The sys.argv
parameter is a list of arguments from a command line.
1 |
myapp = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. also we have created the object of our window class in here.
1 2 3 |
window = Window() myapp.exec_() sys.exit() |
Run the complete code and this will be the result
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email