In this Python GUI article i want to show you how to Create Slider in Pyside2, for creating of Slider in Pyside2, we are going to use QSlider class. also you can watch more articles on Python GUI Development with Pyside2 in the below links. so the slider is the classic widget for controlling a bounded value. It lets the user move a slider handle along a horizontal or vertical groove and translates the handle’s position into an integer value within the legal range.
1: Getting Started With Pyside2 | Qt For Python
2: Pyside2 GUI Creating First Window
3: Pyside2 GUI Creating Window Icon
4: Pyside2 GUI How To Create Icon Modes
5: Pyside2 GUI How To Create Tooltip
6: Pyside2 GUI QPushButton With Signal And Slot
7: Pyside2 GUI Making Center The Window
8: Python GUI How To Create AboutBox
9: Python GUI How to Create Digital Clock in Pyside2
10: How To Create StatusBar In Pyside2
11: Pyside2 Creating QProgressBar
12: Pyside2 Layout Managment with QHBoxLayout
13: Pyside2 GridLayout Example
16: Pyside2 Creating FontComboBox
17: Pyside2 Creating QCompleter
OK now this is the complete code for Python GUI Create Slider in Pyside2
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
from PySide2.QtWidgets import QApplication, QWidget,QHBoxLayout, QLabel, QSlider import sys from PySide2.QtGui import QIcon from PySide2.QtCore import Qt from PySide2 import QtGui class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Pyside2 Slider") self.setGeometry(300,200,300,250) self.setStyleSheet('background-color:red') self.createSlider() self.setIcon() self.show() def setIcon(self): appIcon = QIcon("icon.png") self.setWindowIcon(appIcon) def createSlider(self): hbox = QHBoxLayout() self.slider = QSlider() self.slider.setOrientation(Qt.Horizontal) self.slider.setTickPosition(QSlider.TicksBelow) self.slider.setTickInterval(10) self.slider.setMinimum(0) self.slider.setMaximum(100) self.slider.valueChanged.connect(self.changedValue) self.label = QLabel("0") self.label.setFont(QtGui.QFont("Sanserif", 15)) hbox.addWidget(self.slider) hbox.addWidget(self.label) self.setLayout(hbox) def changedValue(self): size = self.slider.value() self.label.setText(str(size)) myapp = QApplication(sys.argv) window = Window() myapp.exec_() sys.exit() |
So at the top first we have imported our required classed from pyside2, after that we have created our window class that extends from QWidget, and we have added the requirement of our window in that class, also we have called our setIcon() and createSlider() methods in our class.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Pyside2 Slider") self.setGeometry(300,200,300,250) self.setStyleSheet('background-color:red') self.createSlider() self.setIcon() self.show() |
This method is for window icon, make sure that you have added an icon in your working directory.
1 2 3 |
def setIcon(self): appIcon = QIcon("icon.png") self.setWindowIcon(appIcon) |
So now this is the method that we are going to create our slider
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
def createSlider(self): hbox = QHBoxLayout() self.slider = QSlider() self.slider.setOrientation(Qt.Horizontal) self.slider.setTickPosition(QSlider.TicksBelow) self.slider.setTickInterval(10) self.slider.setMinimum(0) self.slider.setMaximum(100) self.slider.valueChanged.connect(self.changedValue) self.label = QLabel("0") self.label.setFont(QtGui.QFont("Sanserif", 15)) hbox.addWidget(self.slider) hbox.addWidget(self.label) self.setLayout(hbox) |
in the above code first of we have created an HBoxLayout, and after that we have created the object of our QSlider class. there are some properties for the QSlider that you can set, for example you can set the orientation, you can set the thick position and you can set the minimum and maximum value for the slider like this .
1 2 3 4 5 6 7 8 |
hbox = QHBoxLayout() self.slider = QSlider() self.slider.setOrientation(Qt.Horizontal) self.slider.setTickPosition(QSlider.TicksBelow) self.slider.setTickInterval(10) self.slider.setMinimum(0) self.slider.setMaximum(100) |
because we are using signal and slot mechanism, by this reason we are going to connect the valueChanged signal of QSlider with changedValue() slot that we are going to make.
1 |
self.slider.valueChanged.connect(self.changedValue) |
Also we need to create a QLabel like this
1 2 |
self.label = QLabel("0") self.label.setFont(QtGui.QFont("Sanserif", 15)) |
In here we are going to add our QSlider and QLabel in our HBoxLayout, that we have created at the top.
1 2 |
hbox.addWidget(self.slider) hbox.addWidget(self.label) |
And this is the slot or method that we have connected this with our valueChanged signal of QSlider at the top.
basically first we are going to get the value from the slider and after that we set the value to our label.
1 2 3 |
def changedValue(self): size = self.slider.value() self.label.setText(str(size)) |
Also every Pyside2 application must create an application object. The sys.argv
parameter is a list of arguments from a command line.
Python scripts can be run from the shell. It is a way how we can control the startup of our scripts
1 |
myapp = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. The mainloop ends if we call the exit()
method or the main widget is destroyed. The sys.exit()
method ensures a clean exit. The environment will be informed how the application ended.
The exec_()
method has an underscore. It is because the exec
is a Python keyword. And thus, exec_()
was used instead
1 2 |
myapp.exec_() sys.exit() |
So now run the complete code and this will be the result
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email