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

Apr 20, 2018 pm 02:18 PM
pyqt5 control

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install pyqt5 How to install pyqt5 Nov 30, 2023 pm 02:05 PM

pyqt5 installation steps: 1. Make sure that Python and pip are installed on the computer; 2. Enter the "pip install PyQt5" command in the terminal or command prompt to install PyQt5; 3. After the installation is complete, you can import the PyQt5 module in the Python script and Get started; 4. You can install some specific functions or components by entering the "pip install PyQt5.QtGui" command; 5. If you encounter any problems, you can try to upgrade pip and setuptools.

Super complete! Python graphical interface framework PyQt5 usage guide! Super complete! Python graphical interface framework PyQt5 usage guide! Apr 13, 2023 am 08:43 AM

Common GUI framework PyQt5: Qt is a cross-platform C++ graphical user interface library. QT was once owned by Nokia and later sold to Digia Oyj, a Finnish software company. PyQt5 is a Python interface based on Digia's Qt5 and consists of a set of Python modules. PyQt5 itself has more than 620 classes and 6000 functions and methods. Can run on multiple platforms, including: Unix, Windows, and Mac OS. Pyside6: Pyside is the Python package officially provided by QT Company. The previous version was Pyside2, which corresponds to QT5. The naming rules of the latest version have been adjusted and changed to Pysid.

How to solve pyqt5 installation error How to solve pyqt5 installation error Nov 20, 2023 pm 02:34 PM

Solution: 1. Check that the Python version matches the requirements of PyQt5; 2. Install pip; 3. Install Qt; 4. Reinstall PyQt5; 5. Check whether dependencies are missing and install the missing dependencies; 6. Check for errors Message, error messages usually provide details about the problem, including missing libraries or modules; 7. Update pip and setuptools; 8. Using a virtual environment, try to install PyQt5 in a virtual environment; 9. Check the network connection; 10. Manual Download and install PyQt5.

Common rich text editor controls in Go language web development framework Common rich text editor controls in Go language web development framework Jun 04, 2023 am 09:10 AM

With the popularity of web applications, rich text editors have become an indispensable tool in web development. When using Go language for web development, we also need to choose a suitable rich text editor control to enrich our websites and applications. In this article, we will discuss common rich text editor controls in Go language web development. FroalaEditorFroalaEditor is a popular rich text editor control that is widely used in web development. it has modernity

How to make excel controls How to make excel controls Mar 20, 2024 am 09:40 AM

When we use the excel office software, if we can use some controls skillfully, it can help us create more professional effects in the excel form. For example, adding selection controls can make the form filler easily complete the form. Below, we will demonstrate how to make an excel selection control. We hope it will be helpful to you! 1. First, we create and open a blank excel table. 2. Add the &quot;Development Tools&quot; tab, click the file button on the upper left side, and find &quot;Excel Options&quot;. After that, we find the development tools in the options of the custom ribbon and check it so that a check mark appears in front of it. 3. Return to the Excel work interface and you can see the &quot;Development Tools&quot; tab. Generally speaking, it is not

How to use panel control How to use panel control Oct 10, 2023 am 09:36 AM

The steps to use the panel control are to first create a Panel control and set its width, height, background color, border color, border width and padding, create two buttons and add them to the Panel control, and finally Add the Panel control to the form.

What are the common Ajax controls? Learn more about its features and capabilities What are the common Ajax controls? Learn more about its features and capabilities Jan 17, 2024 am 11:11 AM

In-depth understanding of Ajax controls: What are the common ones? Introduction: In modern Web development, Ajax (Asynchronous JavaScript and XML) has become a popular technology, which can realize partial refresh of web pages and improve user experience. In development, we usually use Ajax controls to simplify and speed up our development process. This article will take an in-depth look at Ajax controls and introduce some common controls and their functions. 1. jQueryAjax: jQueryA

Revealing the Essential Web Standard Controls: In-depth Analysis of Interface Elements Revealing the Essential Web Standard Controls: In-depth Analysis of Interface Elements Jan 13, 2024 pm 12:19 PM

Web Standard Controls Revealed: Essential Interface Elements Analysis Introduction: Web standard controls are an indispensable part of web design. They provide users with a rich interactive experience and a friendly interface, and play an important role. This article will reveal several essential Web standard controls and analyze their functions and usage methods for readers. 1. Button control Button control is one of the most common and basic elements in Web interface design, used to trigger certain operations. It usually has the following attributes: Text content: the text information displayed on the button. style

See all articles