How to Create SpinBox in Python PySide6

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.

 

 

For creating SpinBox in PySide6, you need to import the required libraries. open your Python code editor and add the following code:

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.

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:

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.

 

 

 

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.

 

 

 

 

This is the complete code for this article

 

 

 

Run the code and this will be the result

How to Create SpinBox in Python PySide6
How to Create SpinBox in Python PySide6

 

 

 

PySide6 GUI Articles

Leave a Comment