Python Forum
Pygame - Images As Buttons
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame - Images As Buttons
#3
You should learn to break it down to parts.
1. You need a surface.
2. You need a position.
3. You need a callback
4. You need event action function.

Now you need a structure to hold data. There many ways to build it and store. Here two ways. Dict and a class. I would recommend the class. But do what you like.

Dict example
def create_button(button, image, position, callback):
    button["image"] = image
    button["rect"] = image.get_rect(topleft=position)
    button["callback"] = callback

def button_on_click(button, event):
    if event.button == 1:
        if button["rect"].collidepoint(event.pos):
            button["callback"](button)

def push_button_goodbye(button):
    print("You push Goodbye button")

# Define your button
button = {}
# Create button
create_button(button, your_image, (10, 10), push_button_goodbye)
# In event loop see if button is trigger. Under pygame.MOUSEBUTTONDOWN.
button_on_click(button, event)
# In main loop draw area.
surface.blit(button["image"], button["rect"])
Class example
class Button:
    def __init__(self, image, position, callback):
        self.image = image
        self.rect = image.get_rect(topleft=position)
        self.callback = callback

    def on_click(self, event):
        if event.button == 1:
            if self.rect.collidepoint(event.pos):
                self.callback(self)

# Define and create button
button = Button(your_image, (10, 10), push_button_goodbye)
# In event loop. Under pygame.MOUSEBUTTONDOWN.
button.on_click(event)
# In main loop draw area.
surface.blit(button.image, button.rect)
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
Pygame - Images As Buttons - by vman44 - Mar-20-2020, 02:16 AM
RE: Pygame - Images As Buttons - by michael1789 - Mar-20-2020, 05:54 AM
RE: Pygame - Images As Buttons - by Windspar - Mar-20-2020, 01:03 PM
RE: Pygame - Images As Buttons - by vman44 - Mar-20-2020, 08:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] pygame-manu : using controller buttons to move around menu mlw19mlw91 2 1,696 Mar-12-2023, 01:55 PM
Last Post: deanhystad
  [PyGame] drawing images onto pygame window djwilson0495 1 3,537 Feb-22-2021, 05:39 PM
Last Post: nilamo
  [pygame] Equiping inventory slots with invisible buttons SheeppOSU 6 4,807 Apr-26-2019, 08:45 PM
Last Post: SheeppOSU
  Problems with loading buttons (pygame) SheeppOSU 2 3,166 Apr-12-2019, 08:04 PM
Last Post: SheeppOSU
  Pygame loads only part of images Prof_Jar_Jar 3 3,148 Dec-22-2018, 03:42 PM
Last Post: Windspar
  Buttons in PyGame mzmingle 4 13,135 Oct-09-2018, 05:19 PM
Last Post: Mekire
  Menus, buttons and other widgets for Pygame Olleus 4 11,551 Apr-17-2017, 11:08 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020