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 8201809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:56:28+00:00 2026-06-07T06:56:28+00:00

I’ve been trying to get this snippet of python code to work, but to

  • 0

I’ve been trying to get this snippet of python code to work, but to no avail. So what I’m trying to do is to make my button have 3 states: idle, hover, and clicked.

However, for some reason the image isn’t changing. Could someone please tell me what I’m doing wrong?

import pygame,sys,os
from pygame.locals import *

BG = "bg.jpg"
BUTTON1 = "buttonidle.png"
BUTTON2 = "buttonhov.png"
BUTTON3 = "buttonclick.png"

def load_image(name, colorkey=None):
    fullname = os.path.join(name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print "Can't load image:", name
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()


class Button(pygame.sprite.Sprite):

    global buttonsprite

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        if buttonsprite == 1:
            self.image,self.rect = load_image(BUTTON1, -1)
        screen = pygame.display.get_surface()
        self.area = screen.get_rect()
        self.rect.topleft = 190, 140

        if buttonsprite == 2:
            self.image,self.rect = load_image(BUTTON2, -1)
        if buttonsprite == 3:
            self.image,self.rect = load_image(BUTTON3, -1)







buttonsprite = 1
pygame.init()
screen = pygame.display.set_mode((500,300))
pygame.display.set_caption('Window')

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255,255,255))

screen.blit(background,(0,0))
pygame.display.flip()

button = Button()
allsprites = pygame.sprite.RenderPlain((button))
allsprites.update()
screen.blit(background,(0,0))
allsprites.draw(screen)
pygame.display.flip()

while True:
    x,y = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    if x >= 193 and x <= 315 and y >= 143 and y <= 171:
        buttonsprite = 2
        if event.type == MOUSEBUTTONDOWN:
            buttonsprite = 3
    else:    
        buttonsprite = 1

Thank you!

EDIT:

Alright, I got some help and I added an update function. But it still doesn’t seem to work.

import pygame,sys,os
from pygame import *

BG = "bg.jpg"
BUTTON1 = "buttonidle.png"
BUTTON2 = "buttonhov.png"
BUTTON3 = "buttonclick.png"

buttonsprite = 1

def load_image(name, colorkey=None):
    fullname = os.path.join(name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print "Can't load image:", name
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()


class Button(pygame.sprite.Sprite):

    global buttonsprite

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image,self.rect = load_image(BUTTON3, -1)
        screen = pygame.display.get_surface()
        self.area = screen.get_rect()
        self.rect.topleft = 190, 140

    def update(self):
        if buttonsprite == 1:
            self.image,self.rect = load_image(BUTTON1, -1)
        if buttonsprite == 2:
            self.image,self.rect = load_image(BUTTON2, -1)
        if buttonsprite == 3:
            self.image,self.rect = load_image(BUTTON3, -1)




pygame.init()
screen = pygame.display.set_mode((500,300))
pygame.display.set_caption('Window')

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255,255,255))

screen.blit(background,(0,0))
pygame.display.flip()

button = Button()
allsprites = pygame.sprite.RenderPlain((button))
allsprites.update()
screen.blit(background,(0,0))
allsprites.draw(screen)
pygame.display.flip()

while True:

    x,y = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == MOUSEBUTTONUP:
            if x>=193 and x<=315 and y>=143 and y<=171:
                buttonsprite = 2
                button.update()
            else:
                buttonsprite = 1
                button.update()
        if event.type == MOUSEBUTTONDOWN:
            if x>=193 and x<=315 and y>=143 and y<=171:
                buttonsprite = 3
                button.update()
            else:
                pass
  • 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-07T06:56:29+00:00Added an answer on June 7, 2026 at 6:56 am

    I would think that it’s because you’re not calling some sort of update function that actively changes the image. If you were to recreate the button after you’d be able to see the change you want.

    Basically, changing the value doesn’t go far enough towards what you want to do, you need to let the Button know that it’s got a new value.

    Bad related example:

    def button():
      global number
      print number
    
    number = 1
    
    button() 
    >>> 1           <----------------↰
    number = 'A STRING NOW'          ↑
    #now the printed string doesn't above change, so you'd have to call button again for it to change:
    button()
    >>> A STRING NOW # <-- BAM
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

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.