鑒於之前學習過Qt的皮毛知識,在此利用學習Python的機會,溫習一下之前所學。本例小試牛刀,根據網上所學(由於時間久遠,記不起根據哪里學的了),熟悉QPushButton的使用,順便學習了在PyQt5中信號與槽的機制。
#-*- coding:utf-8 -*-
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('Red',self)
redb.setCheckable(True)
redb.move(10,10)
redb.clicked[bool].connect(self.setColor)
redb = QPushButton('Green',self)
redb.setCheckable(True)
redb.move(10,60)
redb.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,280,170)
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())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())