in this Python PySide6 article we are going to learn about How to Create SpinBox in Python PySide6, so PySide6 SpinBox is user interface element that provides simple way to allow users to selectvalue from range of predefined values. in Python PySide6 it is popular GUI toolkit that allows developers to create interactive desktop applications. in this article we are going to discuss how to create a SpinBox in PySide6.
How to Create SpinBox in Python PySide6
First of all we need to install PySide6, you can use pip for the installation of PySide6.
1 |
pip install PySide6 |
For creating SpinBox in PySide6, you need to import the required libraries. open your Python code editor and add the following code:
1 2 |
import sys from PySide6.QtWidgets import QApplication, QMainWindow, QSpinBox |
In the above code we have imported QApplication, QMainWindow and QSpinBox classes from the PySide6.QtWidgets module. QApplication class manages GUI application’s control flow and main settings, while the QMainWindow class provides the main application window. QSpinBox class is used to create SpinBox.
Now it is time to create our main window in Python PySide6.
1 2 3 4 |
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("SpinBox Example") |
In the above code we are creating a class called MainWindow that extends from QMainWindow class. in the constructor of the class, we are setting the window’s title to “SpinBox Example”.
Now it is time to create our SpinBox, you can use the following code to create SpinBox with range of 0 to 100 and an initial value of 50:
1 2 3 |
spinbox = QSpinBox(self) spinbox.setRange(0, 100) spinbox.setValue(50) |
So now in the above code we have created QSpinBox object called spinbox and setting its range from 0 to 100 using setRange() method. we are also setting the initial value of SpinBox to 50 using the setValue() method. and finally we are adding the SpinBox to the main window using the self parameter.
So in here we are setting the SpinBox as the central widget of the main window using setCentralWidget() method. and finally we are showing the main window using the show() method.
1 2 |
self.setCentralWidget(spinbox) self.show() |
In this code we are checking if the code is being run as the main module using if name == “main“: statement. after that we are creating an instance of the QApplication class and MainWindow object. and finally we are running the application using app.exec() method and exiting program when the application is closed using the sys.exit() method.
1 2 3 4 |
if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() sys.exit(app.exec()) |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import sys from PySide6.QtWidgets import QApplication, QMainWindow, QSpinBox class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("SpinBox Example") spinbox = QSpinBox(self) spinbox.setRange(0, 100) spinbox.setValue(50) self.setCentralWidget(spinbox) self.show() if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() sys.exit(app.exec()) |
Run the code and this will be the result