In this PyQt6 Tutorial we want to learn how to Create SpinBox in PyQt6, so as we know that PyQt6 is one of the most popular GUI frameworks, It is used for building GUI applications with Python, and PyQt6 SpinBox allows users to select values from a predefined range using up and down arrow buttons.
PyQ6 Installation
First of all you need to install PyQt6, we can use pip for PyQt6 installation.
1 |
pip install PyQt6 |
After installation, we need to import our required modules and classes from PyQt6.
1 2 |
from PyQt6.QtWidgets import QApplication, QMainWindow, QSpinBox, QLabel, QVBoxLayout, QWidget import sys |
After that we need to create an application instance and a main window, which will serve as the container for our SpinBox.
1 2 3 4 |
app = QApplication(sys.argv) window = QMainWindow() window.setWindowTitle("SpinBox Example") window.setIcon(QIcon('codeloop.png')) |
Now, let’s create the SpinBox itself. we want to use QSpinBox class for this purpose, In the above code, we have created an instance of QSpinBox and associate it with our main window. after that we use the setGeometry() method to define the position and size of the SpinBox within the window.
1 2 |
spinbox = QSpinBox(window) spinbox.setGeometry(10, 10, 100, 30) |
For defining the range of selectable values in the SpinBox, we can use setRange() method. we can also set a default value using the setValue() method.
1 2 |
spinbox.setRange(0, 100) spinbox.setValue(50) |
For handling value changes in the SpinBox, we can connect the valueChanged signal to a custom function, in this example handle_value_change function will be called whenever the value in the SpinBox changes, and it will add the value of spinbox in the label.
1 2 3 4 |
def handle_value_change(value): label.setText(f"Selected value: {value}") spinbox.valueChanged.connect(handle_value_change) |
This is the complete code
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 |
from PyQt6.QtWidgets import QApplication, QMainWindow, QSpinBox, QLabel, QVBoxLayout, QWidget import sys from PyQt6.QtGui import QIcon # Function to handle value changes in the SpinBox def handle_value_change(value): # Update label text with label.setText(f"Selected value: {value}") # Create an application instance app = QApplication(sys.argv) # Create main window instance window = QMainWindow() window.setWindowTitle("SpinBox Example") window.setWindowIcon(QIcon('codeloop.png')) # Create a central widget and set it for the main window central_widget = QWidget() window.setCentralWidget(central_widget) # Create a vertical box layout layout = QVBoxLayout(central_widget) # Create a SpinBox and add it to the layout spinbox = QSpinBox() # Set the range of selectable values spinbox.setRange(0, 100) # Set a default value spinbox.setValue(50) layout.addWidget(spinbox) # Create a label to display the selected value label = QLabel("Selected value: 50") layout.addWidget(label) # Connect the valueChanged signal of the SpinBox spinbox.valueChanged.connect(handle_value_change) # Set layout for the central widget central_widget.setLayout(layout) # Show the main window window.show() # Start the application event loop sys.exit(app.exec()) |
Run the complete code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email