我是 Python 新手,使用 PyQt4 开发 Gui。我想在按下切换按钮的同时更改圆圈的颜色。但是我在插槽中遇到错误。
我的代码是:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class MyFrame(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self)
self.scene=QGraphicsScene(self)
self.scene.setSceneRect(QRectF(0,0,245,245))
self.bt=QPushButton("Press",self)
self.bt.setGeometry(QRect(450, 150, 90, 30))
self.bt.setCheckable(True)
self.color = QColor(Qt.green)
self.color1= QColor(Qt.magenta)
self.show()
self.connect(self.bt, SIGNAL("clicked()"), self.changecolor)
def paintEvent(self, event=None):
paint=QPainter(self)
paint.setPen(QPen(QColor(Qt.magenta),1,Qt.SolidLine))
paint.setBrush(self.color)
paint.drawEllipse(190,190, 70, 70)
paint.setPen(QPen(QColor(Qt.green),1,Qt.SolidLine))
paint.setBrush(self.color1)
paint.drawEllipse(300,300, 70, 70)
def changecolor(self):
if pressed:
self.color = QColor(Qt.red)
self.color1= QColor(Qt.blue)
else:
self.color=QColor(Qt.yellow)
self.color1=QColor(Qt.gray)
self.update()
app=QApplication(sys.argv)
f=MyFrame()
f.show()
app.exec_()