Home  >  Article  >  Backend Development  >  PyQt5 must learn the switch button every day_python

PyQt5 must learn the switch button every day_python

不言
不言Original
2018-04-20 14:14:432911browse

This article mainly introduces in detail the relevant information about the switch button that you must learn every day in PyQt5. It has a certain reference value. Interested friends can refer to it

The switch button is a special feature of QPushButton model. It is a button with two states: pressed and unpressed. We modify other content by switching between these two states.


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

"""
PyQt5 教程

在这个例子中,我们创建三个切换按钮。
他们将控制一个QFrame的背景颜色。

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

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFrame
from PyQt5.QtGui import QColor

class Example(QWidget):

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

    self.initUI()

  def initUI(self):

    self.col = QColor(0, 0, 0)

    redb = QPushButton('红', self)
    redb.setCheckable(True)
    redb.move(10, 10)

    greenb = QPushButton('绿', self)
    greenb.setCheckable(True)
    greenb.move(10, 60)

    blueb = QPushButton('蓝', self)
    blueb.setCheckable(True)
    blueb.move(10, 110)

    redb.clicked[bool].connect(self.setColor)
    greenb.clicked[bool].connect(self.setColor)
    blueb.clicked[bool].connect(self.setColor)

    self.square = QFrame(self)
    self.square.setGeometry(150, 20, 100, 100)
    self.square.setStyleSheet('QWidget { background-color:%s }' % 
      self.col.name())

    self.setGeometry(300, 300, 280, 170)
    self.setWindowTitle('切换按钮')    
    self.show()

  def setColor(self, pressed):

    source = self.sender()

    if pressed:
      val = 255
    else:
      val = 0

    if source.text() == '红':
      self.col.setRed(val)
    elif source.text() == '绿':
      self.col.setGreen(val)
    else:
      self.col.setBlue(val)

    self.square.setStyleSheet('QFrame { background-color:%s }' % 
      self.col.name())

if __name__ == '__main__':

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

In our example, we created three toggle buttons and a QWidget. We set the QWidget's background color to black. The toggle button will toggle the red, green and blue parts of the color value. The background color will depend on the toggle.

self.col = QColor(0, 0, 0)

The initial color value is black.

redb = QPushButton('红', self)
redb.setCheckable(True)
 redb.move(10, 10)

Create a toggle button. We create a button using QPushButton and set its setCheckable() method to true.

redb.clicked[bool].connect(self.setColor)

When we click the toggle button a signal is connected to the method we defined. We operate the click signal using a boolean value.

 source = self.sender()

We get the information about the switch button (that is, which button was clicked).

if source.text() == '红':
      self.col.setRed(val)

If it is a red button, we update the red part of the color accordingly.

self.square.setStyleSheet('QFrame { background-color:%s }' % 
    self.col.name())

We use a style sheet to change the background color.

After the program is executed

PyQt5 must learn the switch button every day_pythonPyQt5 must learn the switch button every day_python

Related recommendations:

PyQt5 must learn layout management every day

PyQt5 Must Learn Every Day Check Boxes with Labels

PyQt5 Must Learn Every Day Create Window Centering Effect

The above is the detailed content of PyQt5 must learn the switch button every day_python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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