Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8510613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:45:12+00:00 2026-06-11T03:45:12+00:00

I am new in Python and using PyQt4 for developing the Gui. I want

  • 0

I am new in Python and using PyQt4 for developing the Gui. I want to change the color of circles while pressing the toggle button. But I am getting the error in slot.

My code is:

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_()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:45:13+00:00Added an answer on June 11, 2026 at 3:45 am

    The way it stands, it tries to call changecolor with only one argument, self. I’m not entirely sure what you’re trying to achieve. Your changecolor takes in a variable “paint”, but tries to use self.paint, which doesn’t exist. So maybe you could think, that you can just get a hold of the painter with a call to to QPainter and lose the parameter called “paint”, like following:

    def changecolor(self):
      paint = QPainter(self)
      paint.setBrush(QColor(Qt.red))
      paint.drawEllipse(190,190,70,70)
      self.update()
    

    This runs into the following error:

    QPainter::begin: Widget painting can only begin as a result of a paintEvent
    QPainter::setBrush: Painter not active
    

    That tells you, that you can only have painting actions inside the paintEvent. One solution is to have an class member, e.g. self.color that holds the color you want for the circle. A fully working code is below:

    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.color = QColor(Qt.green)
        self.show()
        self.connect(self.bt, SIGNAL("clicked()"), self.changecolor)
    
      def paintEvent(self, event=None):
        paint=QPainter(self)
        paint.setPen(QPen(QColor(Qt.red),1,Qt.SolidLine))
        paint.setBrush(self.color)
        paint.drawEllipse(190,190, 70, 70)
    
      def changecolor(self):
        self.color = QColor(Qt.red)
        self.update()
    
    app=QApplication(sys.argv)
    f=MyFrame()
    f.show()
    app.exec_()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to selenium and I am using python but I have a
I'm new to Python and am starting to teach myself GUI programming (hopefully) using
I'm currently launching a programme using subprocess.Popen(cmd, shell=TRUE) I'm fairly new to Python, but
I have some code creating a QTabWidget from Python using PyQt4. I want to
I am new to python been using it for graphics but never done it
I am new to python, apologies if this has been asked already. Using python
I'm a new Python programmer who is having a little trouble using 'self' in
I'm trying to launch a website url in a new tab using python in
In my new job more people are using Python than Perl, and I have
I am new to python and I am trying to implement rfind function using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.