The changes in initFormatbar() should be relatively understandable by now. If align = :įmt.setVerticalAlignment()įmt.setVerticalAlignment()įmt.setVerticalAlignment() # And get the vertical alignment property # Set the fontStrikeOut property to its oppositeįmt.setFontStrikeOut(not fmt.fontStrikeOut()) (self.subScript)įurther below: (boldAction) SubAction = QtGui.QAction(QtGui.QIcon("icons/subscript.png"),"Subscript",self) SuperAction = QtGui.QAction(QtGui.QIcon("icons/superscript.png"),"Superscript",self) StrikeAction = QtGui.QAction(QtGui.QIcon("icons/strike.png"),"Strike-out",self) UnderlAction = QtGui.QAction(QtGui.QIcon("icons/underline.png"),"Underline",self) ItalicAction = QtGui.QAction(QtGui.QIcon("icons/italic.png"),"Italic",self) InitFormatbar(): boldAction = QtGui.QAction(QtGui.QIcon("icons/bold.png"),"Bold",self) For backColor, we set the color as the current text's background color. In case of fontColor, we set the color selection as the font color. In both cases, we create two actions that open a QColorDialog when activated. We set the user's selection as the text's current font point size. For the slot function, we again set a second parameter, font size, which PyQt passes to us when the user selects a font size from the combo box or, alternatively, enters a custom size. After connecting the activated signal to a slot function, we populate the combo box with some common font sizes. This is easily done by instantiating a normal combo box, here called fontSize, which we set editable, meaning the user can enter any number they want for the font. PyQt itself doesn't have such a thing, so we need to create one ourself.
Next up, we need a combo box for font sizes. As you can see, we also give this slot function a second parameter font, so PyQt will pass the user-selected QFont object to our function, reducing our work to setting this font to the text's current font. We instantiate it and connect its currentFontChanged signal to a slot function, self.fontFamily(), which we later created underneath the initUI() method. We start out by creating a QFontComboBox, which is a very convenient combo box that automatically includes all the fonts available to the system. We also don't give them tooltips or shortcuts anymore (unless you want to, of course). We don't make these actions class members because we only need to create and use them within the scope of initFormatbar().
PYTHON INDENT BLOCK OF TEXT CODE
Note that the actions we just created don't follow the code pattern for actions I described last time. Self.formatbar = self.addToolBar("Format")īelow initUI(): def fontFamily(self,font): InitFormatbar(): fontBox = QtGui.QFontComboBox(self)į(self.fontFamily)į(self.fontSize)įontSizes = ['6','7','8','9','10','11','12','13','14',įontColor = QtGui.QAction(QtGui.QIcon("icons/font-color.png"),"Change font color",self)į(self.fontColor)īackColor = QtGui.QAction(QtGui.QIcon("icons/highlight.png"),"Change background color",self)ī(self.highlight) Just like last time, I'll only show the functions that change relative to the previous code: We'll start with actions related to font, meaning the user will be able to: This part will focus on the format bar, which we'll populate with a number of features, including actions to change the font family, background color, alignment and more.
PYTHON INDENT BLOCK OF TEXT SERIES
In the previous part of my tutorial series on Building a text editor with PyQt, we created our text editor's basic skeleton and added some useful features for file management, printing, inserting lists and more.
Get started with this series with part one!