Adding a Button¶
In this task a GUI is created containing a single button.
The View¶
The below code creates a QWidget containing a single button. When the button is pressed it will print a message to the terminal screen. It should be noted that in practice this should be avoided and will be discussed in this section.
First we need to import the relevant components from PyQt and other modules.
from qtpy.QtWidgets import QGridLayout, QPushButton, QWidget
from typing import Union
We then create the View class as a QWidget. Each view will have a parent. As a result, the view will automatically be destroyed if the parent is destroyed (unless the view has been removed, via docking, from the parent).
class View(QWidget):
def __init__(self, parent: Union[QWidget, None]=None):
super().__init__(parent)
Next, inside the constructor, we create a layout and add a button to it
grid = QGridLayout()
self._button = QPushButton("Hi", self)
self._button.setStyleSheet("background-color:lightgrey")
# connect button to signal
self._button.clicked.connect(self._button_clicked)
# add button to layout
grid.addWidget(self._button)
# set the layout for the view widget
self.setLayout(grid)
The above connect statement means that when the button is pressed, the
function _button_clicked
is called:
def _button_clicked(self) -> None:
print("Hello world")
The Main¶
To run the GUI we need a ‘main’ module. Assuming that the above has
all been saved in view.py
, the main.py
will contain:
import sys
from qtpy.QtWidgets import QApplication
from view import View
def _get_qapplication_instance() -> QApplication:
if app := QApplication.instance():
return app
return QApplication(sys.argv)
if __name__ == "__main__" :
app = _get_qapplication_instance()
window = View()
window.show()
app.exec_()
Note that there needs to be a QApplication instance running in the background to allow you to show your QWidget.
Tip
Notice we used a plain QWidget instead of QMainWindow to build our widget. This has several advantanges, with the main one being:
A QWidget can be embedded into a larger interface (not possible for a QMainWindow).
A QWidget can be used as a standalone interface (QMainWindow can do this too).
Using QWidget therefore gives you more options for how to use the widget in the future.