A module for use with Pygame. Includes fully customisable buttons, textboxes, sliders and many more, as well as the ability to create and run animations on these widgets.

Overview

Pygame Widgets

A helper module for common widgets that may be required in developing applications with Pygame. It supports fully customisable buttons, collections of buttons, textboxes, sliders and many more! If there are any widgets that you would like to see added, please create an issue!

Changes in Pygame Widgets v1.0.0

In v1.0.0, there are some minor changes to the use of the module, which may affect existing projects. This outlines the changes that will affect current users in the new version.

  • As more widgets are added, importing is now different
# Now
from pygame_widgets.button import Button

# Instead of
from pygame_widgets import Button  # Will not work
  • All widgets are now updated (draw and listen) by the update method
import pygame
import pygame_widgets
from pygame_widgets.button import Button

pygame.init()
win = pygame.display.set_mode((600, 600))
button = Button(win, 100, 100, 300, 150)

run = True
while run:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit()
            run = False
            quit()
            
    win.fill((255, 255, 255))
    
    # Now
    pygame_widgets.update(events)
    
    # Instead of
    button.listen(events)
    button.draw()
    
    pygame.display.update()

Prerequisites

Installation

Ensure that Python 3 and pip are installed and added to your environment PATH.

python -m pip install pygame-widgets

Open a Python console and run the following command.

import pygame_widgets

If you receive no errors, the installation was successful.

Usage

For full documentation, see pygamewidgets.readthedocs.io.

How to Contribute

Any contribution to this project would be greatly appreciated. This can include:

  • Finding errors or bugs and creating a new issue
  • Addressing active issues
  • Adding functionality
  • Improving documentation

If applicable, you should make any changes in a forked repository and then create a pull request once the changes are complete and preferably tested if possible.

Note: If writing any code, please attempt to follow the Code Style Guide

Comments
  • Buttons get clicked when the mouse is pressed outside the button and then dragged into the button area.

    Buttons get clicked when the mouse is pressed outside the button and then dragged into the button area.

    How to reproduce:

    1. Press the mouse button outside any widget button area.
    2. Drag the mouse (with the mouse button still pressed) into a widget button num. 1 area.
    3. Observe that the onClick function is called for button num. 1.
    4. With the mouse button still pressed keep dragging into another button.
    5. Observe that the onClick function is called for button num. 2.

    Video illustrating the issue

    bug 
    opened by tgonzalez89 9
  • Documentation refactoring

    Documentation refactoring

    The documentation is becoming difficult to navigate.

    Perhaps it's time to put a navigation system in place to help people finding what they need

    I can do it if you need

    documentation 
    opened by slashformotion 7
  • Dropdown

    Dropdown

    Dropdowns would be a great widget to add. Some things to perhaps keep in mind:

    • Setting a minimum size or number of options visible
    • Scrolling, if there are more options than the specified range
    • Formatting the text (left/right justified, centered)
    • Direction of the dropdown: U/down/left/right
    enhancement 
    opened by Kaif-Kutchwala 4
  • Width and height should be optional

    Width and height should be optional

    I think width and height of widgets should be optional when margin is given,or them should be optional even if margin is not given, and decided by font size,then widget would be more flexible,do hope awesome update. @AustL Regards larryw3i.

    bug 
    opened by larryw3i 3
  • Support for drawing in any surface.

    Support for drawing in any surface.

    Right now the code expects the widget to be drawn in the display surface. If I draw it in another surface it doesn't understand the widget's position relative to the top level display surface only to the "local" surface and it doesn't handle mouse events properly.

    opened by tgonzalez89 3
  • Possible bugs in TextBox widget, regarding font and input text

    Possible bugs in TextBox widget, regarding font and input text

    Hi! I have encountered two possible bugs/issues when using the textbox widget:

    1. Custom font TypeError:

    When I pass a custom font as an argument to the init() method of textbox, I get a TypeError: 'pygame.font.Font' object is not iterable. I’ve looked at self.font inside the class and I’ve changed it from : self.font = pygame.font.SysFont(kwargs.get('font', 'sans-serif'), self.fontSize) to:
    self.font = kwargs.get('font', pygame.font.SysFont('sans-serif', self.fontSize)), and it worked great.

    2. IndexError when the cursor is at the end of the input text:

    When the blinking cursor is at the end of the text, before submitting it to getText() method, after I press enter, I get an IndexError in self.draw() :

    (x[self.cursorPosition], self._y + self.cursorOffsetTop),
    IndexError: list index out of range
    

    I’ve managed to fix this one too, using a basic try and except, in self.draw():

    if self.showCursor:
                    try:
                        pygame.draw.line(
                            self.win, (0, 0, 0),
                            (x[self.cursorPosition], self._y + self.cursorOffsetTop),
                            (x[self.cursorPosition], self._y + self._height - self.cursorOffsetTop)
                        )
                    except IndexError:
                        self.cursorPosition -= 1
    

    This only works when the cursor is at the end of the given input, but I have experienced similar problems when it’s at the beginning of the text, or when passing in an empty string : ‘ ‘ to self.setText().

    You should be able to fix those issues without much of a problem.

    bug 
    opened by bowpie 3
  • Add a waiting bar / progress bar widget.

    Add a waiting bar / progress bar widget.

    A widget like a graphical progress bar (or other graphic element) based on values supplied by user code. Could be used as load waiting progress, other progress indicator, player health, etc.

    enhancement 
    opened by deegquest 2
  • Checkbox not compatible with 1.9.6 pygame.draw

    Checkbox not compatible with 1.9.6 pygame.draw

    I understand that the rounded corners of Pygame 2.0.0 are really nice, but it's not released yet. This component is more dependent on it, but I dont mind square checkboxes for now.

    I attempted to fix the selection.py::Checkbox::draw() function but it didnt show properly.

        def draw(self):
            """ Display to surface """
            if not self.hidden:
                for row in range(self.rows):
                    colour = self.colour1 if not row % 2 else self.colour2
                    if row == 0:
                        draw_rounded_rect(
                            self.win, colour, Rect(self.x, self.y + self.rowHeight * row, self.width, self.rowHeight),
                            border_radius=self.radius #, border_top_right_radius=self.radius
                        )
    
                    elif row == self.rows - 1:
                        draw_rounded_rect(
                            self.win, colour, Rect(self.x, self.y + self.rowHeight * row, self.width, self.rowHeight),
                            border_radius=self.radius #, border_bottom_right_radius=self.radius
                        )
    
                    else:
                        draw_rounded_rect(
                            self.win, colour, Rect(self.x, self.y + self.rowHeight * row, self.width, self.rowHeight)
                        )
    
                    width = 0 if self.selected[row] else self.boxThickness
                    # draw_rounded_rect(
                    #     self.win, self.boxColour,
                    #     self.boxes[row],
                    #     border_radius=width
                    # )
    
                    self.win.blit(self.texts[row], self.textRects[row])
    
    opened by davidzwa 2
  • SyntaxError: invalid syntax - pygame 1.9.6

    SyntaxError: invalid syntax - pygame 1.9.6

    Traceback (most recent call last):
      File "VENV_PATH_HERE/tryouts/pygame-matplotlib.py", line 5, in <module>
        from pygame_widgets import Button
      File "VENV_PATH_HERE\lib\site-packages\pygame_widgets\__init__.py", line 2, in <module>
        from pygame_widgets.button import Button, ButtonArray
      File "VENV_PATH_HERE\lib\site-packages\pygame_widgets\button.py", line 160
        if (parent := super().get(attr)) is not None:
                   ^
    SyntaxError: invalid syntax
    

    Maybe not python 3 compatible or not 3.7 at least?

    opened by davidzwa 2
  • Add accessor for widget visibility

    Add accessor for widget visibility

    As far as I can tell, there's no way to tell if a widget is currently visible aside from the private _hidden attribute. A property or function like is_visible would be helpful to have so that the private attribute does not need to be used.

    opened by notmatthancock 1
  • Adding a search bar

    Adding a search bar

    Hi,

    I really enjoy that library !

    I wanted to add a search bar widget. I tried to stay compliant with how the library works. Also tried to include the DropDown menu and TextBox functionalities to ease the implementation.

    Added one py file and one md file of the doc, with an example gif where we can search a color among all the available ones in pygame.

    searchbox

    Also fixed few things from the drop down menu:

    • elements where clickable when hidden)
    • selection of element occured on mouse pressed instead of mouse release (click instead of clicked)

    Addition to TextBox:

    • onTextChanged callback which could be useful for other application as well

    Fixed imports

    • Should use relative imports in package

    Let me know if anything should be added/changed

    opened by lionel42 1
  • No module named 'animation'

    No module named 'animation'

    Describe the bug Hi, I tried to import pygame_widgets.animations's function, Resize, and it seems that the animation module is not found.

    Screenshots image

    Version Numbers

    • Pygame Widgets 1.1.0
    • Pygame 2.1.3.dev8
    • Python 3.11
    bug 
    opened by OrangeLeafDev 0
  • There don't seem to be any type hints

    There don't seem to be any type hints

    I'm very new to python, pygame, etc, so maybe I missed something big. I'm using pylance and mypy to do static analysis, and they don't seem to be finding a type library or any type hints. Pygame itself seems to have these type hints.

    Is there an install step I missed or is it just not in the library?

    If it's not in the library, would you be interested in a PR that adds the type hints? Maybe I could contribute...

    bug documentation 
    opened by SteveBenz 1
  • [IDEA] Let's create a CHANGELOG.md

    [IDEA] Let's create a CHANGELOG.md

    Check this out : https://keepachangelog.com/en/1.1.0/ I think this could really benefit the project.

    Here is a personal exemple https://github.com/slashformotion/hugo-tufte/blob/master/CHANGELOG.md

    documentation 
    opened by slashformotion 0
Releases(v1.1.0)
  • v1.1.0(Sep 25, 2022)

  • v1.0.0(Aug 4, 2021)

    Pygame Widgets v1.0.0

    This is the official new release of Pygame Widgets!

    Installation

    To install the new version, run the following command in a terminal window. pip install pygame-widgets==1.0.0

    Usage

    After creating your widgets the usual way, you no longer need to call their listen and draw methods every loop. Instead, simply add:

    pygame_widgets.update(events)

    at the end of the main loop and this will handle all of that. Simply call the disable or hide method if you don't want the widget to listen or draw:

    widget.disable() or widget.hide()

    Note: Documentation is now available on readthedocs.io.

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0-beta(Jul 29, 2021)

    Pygame Widgets v1.0.0-beta

    This pre-release implements a new system for handling mouse input and widgets.

    Installation

    To install this pre-release use the terminal command:

    pip install pygame_widgets==1.0.0b0

    Usage

    After creating your widgets the usual way, you no longer need to call their listen and draw methods every loop. Instead, simply add:

    pygame_widgets.update(events)

    at the end of the main loop and this will handle all of that. Simply call the disable or hide method if you don't want the widget to listen or draw:

    widget.disable() or widget.hide()

    Note: Documentation will be made available as soon as possible.

    Source code(tar.gz)
    Source code(zip)
  • v0.6.0(Jul 25, 2021)

  • v0.40(Feb 20, 2021)

  • v0.30(Feb 14, 2021)

  • v0.2.1(Sep 1, 2020)

Owner
I'm a high school student who creates mainly Python and Java projects.
Wordle-helper: python script to help solving wordle game

wordle-helper This is a python script to help solving wordle game 5-letter-word-

MD Nur Ahmed 2 Feb 08, 2022
Snake (PyGame-based) port for Minecraft:Bedrock Edition using PEWSAPI

Snake_PEWSAPI Snake (PyGame-based) port for Minecraft:Bedrock Edition using PEWSAPI And we are not going to make any change to the original Snake sour

Azuki 1 Mar 17, 2022
A set of functions compatible with the TIC-80 platform

Pygame-80 A set of functions from TIC-80 tiny computer platform ported to Pygame 2.0.1. Many of them are designed to work with the NumPy library to im

7 Aug 08, 2022
Just a copied of flappy bird game made by Thuongton999

flappy-bird Just a copied of flappy bird game made by Thuongton999 Installation and Usage Using terminal (on Window) Make sure you installed Python an

ThuongTon 9 Aug 08, 2021
Searches the word list in Wordle based on search pattern.

Wordle Searcher Searches the word list in Wordle based on search pattern. Warning: like all forms of cheating, it trivializes the game, and robs you o

Tyler Martin 1 Jan 29, 2022
Made by Ashish and Avinash-sord12k. Powered by pygame

Spook_alle About -Made by Ashish (Github: Ashish-Github193) and Avinash-sord12k Version - BETA v_1.0 /1-11-2021/ (game is at its base version more ite

Ashish Kumar Jha 1 Nov 01, 2021
A near-exact clone of google chrome's no internet game, or the "google dinosaur game", with some additions and extras.

dinoGame A near-exact clone of google chrome's no internet game, or the "google dinosaur game", with some additions and extras. Installation Download

1 Oct 26, 2021
A simple python script to pregenerate minecraft worlds.

mcloady mcloady is a lightweight python script used to pre-generate Minecraft terrain using MCRcon and carpet mod (optional). Inspired by Pre-Generati

5 Dec 08, 2021
In the works, creating a new Chess Board and way to Play...

sWJz4KingsChess date started on github.com 11-13-2021 In the works, creating a new Chess Board and way to Play... starting to write this in Pygame, an

Shawn 2 Nov 18, 2021
Hexagon game. Two players: AI and User. Implemented using Alpha-Beta pruning to find optimal solution for agent.

Hexagon game. Two players: AI and User. Implemented using Alpha-Beta pruning to find optimal solution for agent.

Anton 1 Oct 18, 2021
Flappy bird using Pygames

flappy-bird Esse é um jogo que eu fiz utilizando a biblioteca de jogos do Python

Leandro Henrique 2 Jan 05, 2022
An entropy-based strategy to wordle

An entropy-based strategy to wordle

Gilles Vandewiele 24 Dec 31, 2022
A Game Engine Made in Python with the Pygame Module

MandawEngine A Game Engine Made in Python with the Pygame Module Discord: https://discord.gg/MPPqj9PNt3 Installation To Get The Latest Version of Mand

Mandaw 14 Jun 24, 2022
A simple pygame implementation of the LOGO programming language.

LOGO-py A simple pygame implementation of the LOGO programming language. Latest Version Notes Fixed a bug where penup/pendown would not work properly.

Ethan Evans 1 Dec 05, 2021
Crazy fast kahoot game flooder with a GUI and multi OS support.

kahoot flooder Crazy fast kahoot game flooder with a GUI and multi OS support. Made in python using tkinter and KahootPY with toast notifications. Req

Ben Tettmar 1 Apr 09, 2022
You want to uto-update your private minecraft client? Give this to developer and enjoy!

minecraft-hack-installer You want to uto-update your private minecraft client? Give this to developer and enjoy! Steps to do: Install libraries: pip i

EuropeStudio 1 Jun 03, 2022
A Frogger game in Python with pygame

CrockiCrocki A personal project of a simple "game" in Python to learn Pygame and Python. Requires: pygame: In Linux: pip3 install pygame In MacOS: pip

Jorge Abreu 1 Nov 07, 2021
Aftermath is an anti token grabber written in Python3.

🎈 Aftermath 🎈 Aftermath is an anti token grabber written in Python3. This tool with a GUI setup is checking the Downloads folder to search for token

Billy 39 Dec 16, 2022
Chess game for 2 players with UI

2-D Chess for 2 players Overview This is a simple 2D Chess game for 2 players using pygame library. Requirements To be able to run this game, you must

7 Apr 06, 2022
Ultimaze est un jeu en 2.5D, réalisé dans le cadre d'un projet de NSI.

Ultimaze Ultimaze est un jeu en 2.5D, réalisé dans le cadre d'un projet de NSI. La consigne était d'utiliser la librairie pygame pour créer un jeu en

parlabarbedeMerlin 3 Sep 17, 2022