Python 3 PyQt5 QPushButton Widget Example to Toggle Buttons and Background Color Values GUI Desktop App

You are currently viewing Python 3 PyQt5 QPushButton Widget Example to Toggle Buttons and Background Color Values GUI Desktop App

Python 3 PyQt5 QPushButton Widget Example to Toggle Buttons and Background Color Values GUI Desktop App

from PyQt5.QtWidgets import (QWidget, QPushButton,
                             QFrame, QApplication)
from PyQt5.QtGui import QColor
import sys
 
 
class Example(QWidget):
 
    def __init__(self):
        super().__init__()
 
        self.initUI()
 
    def initUI(self):
 
        self.col = QColor(0, 0, 0)
 
        redb = QPushButton('Red', self)
        redb.setCheckable(True)
        redb.move(10, 10)
 
        redb.clicked[bool].connect(self.setColor)
 
        greenb = QPushButton('Green', self)
        greenb.setCheckable(True)
        greenb.move(10, 60)
 
        greenb.clicked[bool].connect(self.setColor)
 
        blueb = QPushButton('Blue', self)
        blueb.setCheckable(True)
        blueb.move(10, 110)
 
        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, 300, 250)
        self.setWindowTitle('Toggle button')
        self.show()
 
    def setColor(self, pressed):
 
        source = self.sender()
 
        if pressed:
            val = 255
        else:
            val = 0
 
        if source.text() == "Red":
            self.col.setRed(val)
        elif source.text() == "Green":
            self.col.setGreen(val)
        else:
            self.col.setBlue(val)
 
        self.square.setStyleSheet("QFrame { background-color: %s }" %
                                  self.col.name())
 
 
def main():
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
 
 
if __name__ == '__main__':
    main()



Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply