To use the modern and powerful Qt library, you need to install the PyQt6 package. To do this, call up the command Install tools/install packages with PIP via the GuiPy tools menu and enter PyQt6 as the package name. You can use the pip list command to display the list of currently installed packages.
In addition, you should install the PySide6 package in order to be able to run source code based on it.
One of the major changes in PyQt6 is the need to use fully qualified names for enums and flags. For example in PyQt5 and PySide2 you could just write Qt.DecorationRole or Qt.AlignLeft. This shortened form no longer works in PyQt6, you now have to write Qt.ItemDataRole.DisplayRole or Qt.Alignment.AlignLeft. This change affects all enums and flag types in Qt. Both long and short names are still supported in PySide6.
If you prefer to work with the shortened forms, you can replace PyQt6 with PySide6 in the Qt template. The Qt template can be found in the configuration under Editor/File templates.
A GUI form is built with the help of widgets. These are the graphical components available in both Qt toolbars. The basics for using widgets are given below, which is sufficient in many cases. More detailed information can be found e.g. in the Qt Reference.
A GUI program is created using the icon for new Qt application on the Program tab. Widgets can be placed by drag and drop or by clicking on a widget and then clicking in the GUI form. Only absolute layout is supported. This is sufficient for school purposes. Designing a GUI surface with layout managers is much more difficult.
Attributes and signals of a widget are configured in the object inspector. Initially, only the most important attributes and signals are displayed. This filtering simplifies working with the object inspector. You can display more or all attributes and signals in two further stages.
The Python code of a GUI program is saved with the .pyw file extension, the associated form with the .pfm file extension. Both will open together. If you close the form, you can open it again using the icon in the editor toolbar.
A Label widget is used to label widgets of a GUI form. In addition to text, a label can also display an image or both. To display an image, select the desired image file in the object inspector for the Pixmap attribute.
An image can be animated using the move(x, y) method of the Label widget. The car example shows how this works.
EAN = self.leEAN.text()
To enter a number, convert the entered text into an integer with int() or into a decimal with float().
Amount = float(self.leAmount.text())
If a number is to be output, it must be converted into a string using the str() function.
In contrast to the LineEdit widget, a PlainTextEdit widget displays a multi-line text. In the object inspector, you can enter the text to be displayed using the PlainText attribute. At runtime, you can use the appendPlainText() method to output line by line to a PlainTextEdit widget named Output.
def output(self, line): self.Output.appendPlainText(line)
clear() clears all text. The documentation shows other methods.
def pushButton1_clicked(self, ckecked): # ToDo insert source code here pass
In order to keep track of the source text even when there are many buttons in a form, the buttons and associated event methods are automatically named according to the label in the Text attribute of the object inspector.
Double-clicking on a button in the design window positions the cursor on the associated event method.
if self.cbSelected.isChecked(): print('Selected!')
A ButtonGroup offers several choices from which you can select an option. A newly created ButtonGroup automatically receives a group with the three options „America, Europe and Asia“ and the label „ Continent “. In the object inspector, you can find these values in the Items or Title attribute and can easily adjust them there.
The radio buttons of a button group are numbered consecutively from 0. With checkedId() or setId() you can query the number of the selected radio button or select a radio button. In source code, these methods are called via the non-visual Buttongroup widget whose name ends with „BG“. A GroupBox widget is responsible for the visual appearance with frame and label.
If you set the Checkboxes attribute to true in the object inspector, the radio buttons are converted into checkboxes. Many options can be displayed in multiple columns using the Columns attribute.
A ListWidget displays a list of strings that can be entered in the object inspector via the Items attribute. The number of the selected row is obtained via currentRow(), the currently selected string with currentItem().text(). An entry can be changed using item(Nr).setText().
def pushButton1_clicked(self, checked): print(self.listWidget1.currentRow()) print(self.listWidget1.currentItem().text()) self.listWidget1.item(2).setText('Afrika') pass
For more ListWidget methods, see https://doc.qt.io/qt-6/qlistwidget.html
A ComboBox is a combination of an input line and a drop-down list box. At runtime, the user can enter text in the entry line or select a list item. In the object inspector, you can use the ListItems attribute to enter strings in the selection list.
print(self.comboBox1.currentText()) print(self.comboBox1.currentIndex()) print(self.comboBox1.itemText(index)) self.comboBox1.setItemText(2, 'Australia')
With a SpinBox you can select numbers from a range step by step. In the object inspector you define the minimum, maximum, increment (single step) and current value (value). The current value can be queried in a program with value() and set with setValue().
The tab „Qt Controls“ contains a DoubleSpinBox widget for decimal values.
A ScrollBar can be used to scroll, in which the visible section of a displayed text or graphic is moved. Some widgets such as B. PlainTextEdit, ListBox or TableWidget are automatically provided with scrollbars.
def pushButton1_clicked(self, checked): self.canvas1Painter.drawEllipse(0, 0, 100, 100) # draw a circle self.canvas1Painter.drawLine(0, 100, 100, 0) # draw a line self.canvas1.setPixmap(self.canvas1Pixmap) # show the drawing
A Canvas consists of a Label widget that hosts the canvas. Then we have a pixmap the size of the Label, which is the actual drawing surface. And third, we have the QPainter that draws on the pixmap.
A shown in the example, you can easily draw on the canvas with the painter. When the drawing is finished, you have to show it with
A Frame wigdet is a container for other widgets. Frames are a good way to structure graphic interfaces. To place a widget in a frame, click it in the toolbar and then click in the frame, or drag and drop a widget from the toolbar over the frame. If you move a frame, the widgets it contains are also moved.
GuiPy graphically displays a ButtonGroup for RadioButtons or CheckBoxes using a GroupBox.
The Menu is defined via the MenuItems attribute, in which the menu structure is generated by appropriate indentation. With a newly created menu, the structure looks like this:
File New Python XML Load Save, Ctrl+S Edit Copy, Ctrl+C Paste, Ctrl+V - Delete
Event methods are created for the menu items, which are called when the menu item is selected.
def menuBar1FileNewMenuPythonCommand(self): # ToDo insert source code here pass
You can assign a context menu to each widget. To do this, you specify the name of the menu in the ContextMenu attribute of the widget.
self.tabWidget1Page1 = QLabel() self.tabWidget1Page1.setText('Add widgets to this page') self.tabWidget1.addTab(self.tabWidget1Page1, 'Tab 1')
The structure is defined via the Items attribute by appropriate indentation. This structure is initially specified:
First, F2, F3 node A, A2, A3 node B Second node C node D, D2
A TextEdit widget displays a multi-line text like the PlainTextEdit widget, but in HTML or Markdown format. The HTML text to be displayed can be entered in the object inspector via the Html attribute. In a program, the text can be read with toHTML() and written with setHTML() at runtime. Other methods are described in the documentation.
A ToolButton is a special button that provides quick access to specific commands or options. Unlike a normal button, a ToolButton does not typically display a text label, but instead displays an icon. Usually ToolButtons are placed on ToolBars.
DateTimeEdit allows the user to edit dates using the keyboard or arrow keys to increase or decrease date and time values. The arrow keys can be used to move from section to section within the QDateTimeEdit field.
A ScrollArea is used to display the content of a child widget within a frame. If the widget exceeds the size of the frame, the view can provide scroll bars so that the entire area of the child widget can be displayed. The child widget must be specified with setWidget().