PyQt5 must learn the switch button every day_python

不言
Release: 2018-04-20 14:14:43
Original
2722 people have browsed it

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_())
Copy after login

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)
Copy after login

The initial color value is black.

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

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

redb.clicked[bool].connect(self.setColor)
Copy after login

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()
Copy after login

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

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

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())
Copy after login

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!

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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!