Day 20 | day 28 learn PyQt5, slide control

The slider QSlider control provides a vertical or horizontal slider. The slider is a typical control used to control bounded values. It allows users to move the slider in a certain range along the horizontal or vertical direction, and convert the position of the slider to an integer value within a legal range. Sometimes this method is more natural than entering numbers.

The common event types of QSlider control are as follows.

Event type

describe

vlaueChanged

This type is most commonly used to trigger an event when the value of the slider changes

sliderPressed

Event triggered when slider is pressed

sliderMoved

Event triggered when slider is dragged

slierReleased

Event triggered when slider is released

The slider control QSlider can be displayed horizontally or vertically. Just set different values in the constructor. Qt.Horizontal represents horizontal and Qt.Vertical represents vertical. The common methods of QSlider control are shown in the following table.

method

describe

setMinimum()

Sets the minimum value for the slider control

setMaximum()

Sets the maximum value for the slider control

setSingleStep()

Sets the step size of the slider control

setValue()

Sets the value of the slider control

value()

Gets the value of the slider control

setTickInterval()

Set scale interval

setTickPosition

(QSlider_TickPosition)

To set the position of the scale mark, you can enter an enumeration value. This enumeration value specifies that the scale mark is equivalent to the position of the slider and user operation. You can select an enumeration value and look down.

QSlider_ The optional enumeration values of tickposition are shown in the following table.

enum

describe

QSlider.NoTicks

No tick marks are drawn

QSlider.TicksBothSides

Draw tick marks on both sides of the slider

QSlider.TicksAbove

Draw a tick mark above the horizontal slider

QSlider.TicksBelow

Draw a tick mark below the horizontal slider

QSlider.TicksLeft

Draw a tick mark to the left of the vertical slider

QSlider.TicksRight

Draw a tick mark to the right of the vertical slider

Program list: slider.py

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget, QLabel,
  QSlider, QSpinBox, QVBoxLayout
from PyQt5.QtCore import Qt


# Inherit QWidget
class SliderWidget(QWidget):
    info_lbl = None

    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        # Vertical layout
        v_box = QVBoxLayout()
        self.info_lbl = QLabel("The current value of the slider control is:10")
        v_box.addWidget(self.info_lbl)
        # Create a horizontal slider
        slider = QSlider(Qt.Horizontal)
        slider.setProperty("id", 1)
        slider.setMinimum(10)  # Set minimum value
        slider.setMaximum(100)  # Set maximum
        slider.setSingleStep(10)  # step
        slider.setValue(20)  # set currency
        slider.setTickInterval(10)  # Set scale spacing
        slider.valueChanged.connect(self.valuechange)
        v_box.addWidget(slider)
        self.setLayout(v_box)
        # resize window
        self.resize(600, 200)
        # Center window
        self.center()
        # Window title
        self.setWindowTitle("Application of slider")
        # Display window
        self.show()

    def valuechange(self):
        sender = self.sender()
        self.info_lbl.setText("The current value of the slider control is:%d" % sender.value())

    # Achieve centering
    def center(self):
        f = self.frameGeometry()
        c = QDesktopWidget().availableGeometry().center()
        f.moveCenter(c)
        self.move(f.topLeft())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = SliderWidget()
    sys.exit(app.exec_())

After running the program, the pop-up window is as follows:

Well, that's all for the slider control. Pay attention to me and the next section will be more exciting.

Codeword is not easy, your attention and forwarding is my greatest encouragement, thank you!

Today's headline: Lao Chen said that Python will be fully shared by 2021 national day. The complete courses include:
1. 12 days
2. Data analysis in 16 days
3. Python web crawler in 10 days
4. Django3.0 project practice
5. Wxpython in 25 days
6. The 28 day society PyQt5 is being released
7. The 25 day society Seaborn data analysis was released on csdn
8. Three day Pyecharts data analysis was released during the national day

Tags: Python Data Analysis PyQt5

Posted on Wed, 29 Sep 2021 23:11:23 -0400 by Kurrel