Home > Backend Development > Python Tutorial > PyQt5 must learn the slider control QSlider_python every day

PyQt5 must learn the slider control QSlider_python every day

不言
Release: 2018-04-20 14:18:16
Original
7095 people have browsed it

This article mainly introduces the slider control that you must learn every day in PyQt5. It has a certain reference value. Interested friends can refer to it

QSlider is a handle that can be pulled back and forth. of controls. Sometimes using a slider is more convenient than entering numbers or using a spin box.

In our example we will create a slider and a label. Label display image. The slider will control the image displayed by the label.

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
PyQt5 教程

这个例子显示了一个QSlider控件的使用方法。

作者:我的世界你曾经来过
博客:http://blog.csdn.net/weiaitaowang
最后编辑:2016年8月3日
"""

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QSlider
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt

class Example(QWidget):

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

  self.initUI()

 def initUI(self):

  sld = QSlider(Qt.Horizontal, self)
  sld.setFocusPolicy(Qt.NoFocus)
  sld.setGeometry(30, 40, 100, 30)
  sld.valueChanged[int].connect(self.changeValue)

  self.label = QLabel(self)
  self.label.setPixmap(QPixmap('F:\Python\PyQt5\Widgets\images\mute.png'))
  self.label.setGeometry(160, 30, 80, 50)

  self.setGeometry(300, 300, 280, 170)
  self.setWindowTitle('滑块控件')  
  self.show()

 def changeValue(self, value):

  if value == 0:
   self.label.setPixmap(QPixmap('F:\Python\PyQt5\Widgets\images\mute.png'))
  elif value > 0 and value <= 30:
   self.label.setPixmap(QPixmap(&#39;F:\Python\PyQt5\Widgets\images\min.png&#39;))
  elif value > 30 and value < 80:
   self.label.setPixmap(QPixmap(&#39;F:\Python\PyQt5\Widgets\images\med.png&#39;))
  else:
   self.label.setPixmap(QPixmap(&#39;F:\Python\PyQt5\Widgets\images\max.png&#39;))

if __name__ == &#39;__main__&#39;:

 app = QApplication(sys.argv)
 ex = Example()
 sys.exit(app.exec_())
Copy after login

In our example, we simulate a volume control. By dragging the slider's handle, we change the image on the label.

sld = QSlider(Qt.Horizontal, self)
Copy after login

Create a horizontal slider QSlider

self.label = QLabel(self)
self.label.setPixmap(QPixmap(&#39;F:\Python\PyQt5\Widgets\images\mute.png&#39;))
Copy after login

Create a label QLabel control and Set the initial image to be displayed

sld.valueChanged[int].connect(self.changeValue)
Copy after login

Connect the slider's valueChanged signal to the changeValue() method (slot)

if value == 0:
 self.label.setPixmap(QPixmap(&#39;F:\Python\PyQt5\Widgets\images\mute.png&#39;))
Copy after login

We set the image on the label based on the value of the slider. In the above code, if the slider equals zero the label's image is set to mute.png.

After the program is executed

PyQt5 must learn the slider control QSlider_python every dayPyQt5 must learn the slider control QSlider_python every day

Related recommendations:

##PyQt5 must learn the switch button every day_python

PyQT realizes multi-window switching

PyQt5 events and signals that must be learned every day

The above is the detailed content of PyQt5 must learn the slider control QSlider_python every day. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template