Open YunchaoYang opened 11 months ago
Go to https://www.qt.io/download and download the Qt package. You can opt to install only Creator during the installation.
Open up Qt Creator and you will be presented with the main window. The designer is available via the tab on the left hand side. However, to activate this you first need to start creating a .ui
file.
PyQtGraph: Scientific Graphics and GUI Library for Python:
VisPy[https://vispy.org/]
Slots is the name Qt uses for the receivers of signals. In Python any function (or method) in your application can be used as a slot -- simply by connecting the signal to it. If the signal sends data, then the receiving function will receive that data too. Many Qt widgets also have their own built-in slots, meaning you can hook Qt widgets together directly.
button.clicked.connect(self.the_button_was_clicked)
. Here we create a simple custom slot named the_button_was_clicked which accepts the clicked signal from the QPushButton button = QPushButton("Press Me!")
button.setCheckable(True)
button.clicked.connect(self.the_button_was_toggled)
def the_button_was_toggled(self, checked):
print("Checked?", checked)
button.setChecked(self.button_is_checked)
QMouseEvent
object, with information about the event being readable from the following event methods.Context menus are small context-sensitive menus which typically appear when right clicking on a window.
PyQt5
1. PyQt Concepts and Programs
2. Beyond empty windows
Next, we will look at how to make a widget inside another widget to make a bit complex interface design.
label = QLabel(w) btn = QPushButton(w)
QLabel is used to print non-editable text or placeholders inside a widget, whereas QPushButton is used to create a clickable button. The critical thing to notice here is that when you create the label and btn objects, you are passing the window object (w) to the constructors of QLabel and QPushButton. This is how nesting works in PyQt5. To create a widget inside another widget, you pass the reference of the parent widget to the child’s constructor.move()
is used to set the position of a widget with respect to its parent widget. In the first case, the label will be moved 100px from the left and 130px from the top of the window. 5.. The dialog method is called from the main block of the program when a button is pressed in a specific widget (in this case, the btn.PushButton). The click event triggered on that button causes this function to execute. Such a function is called aslot
in Qt, and you will learn more about signals and slots in the upcoming paragraphs.btn.clicked.connect(dialog)
Finally, this is an example of signals and slots in Qt. In GUI based applications, functions are executed based on the actions performed by the user, like hovering over an element or clicking a button. These actions are called events. Recall that theapp.exec_()
method transfers control to the Qt event-loop.widget.signal.connect(slot)
Components and Widgets
GUI apps has plenty of widgets in a PyQt app. To grasp a clear picture of a PyQt5 code, it is goo dto have a organized architectural view of the widget, modules, libraries, APIs.
The most frequently used widgets:
Layouts and Themes
Layouts
QBoxLayout
is used to align the child widgets of the layout in a horizontal or vertical row. The two classes of interest that inherit from QBoxLayout are:QHBoxLayout
: used to line the child widgets horizontally.QVBoxLayout
: used to line the child widgets vertically.QGridLayout
QGridLayout is used to create interfaces in which the widgets are laid out in the form of a grid (like a matrix or 2D array).Themes
references:
pyQt5 templates