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.
null
PyGame-Tutorial - Refrence for building games in pygame

PyGame-Tutorial How to build games using the python library PyGame End result Ho

St. Mark's Computer Science Club 2 Jan 9, 2022
Pyvidplayer - An extremely easy to use module that plays videos on Pygame

pyvidplayer An extremely easy to use module that plays videos on Pygame Example

null 17 Dec 5, 2022
A Pygame Hangman Game coded in Python 3. Run Hangman.py in a terminal if you have Python 3

Hangman A Pygame Hangman Game coded in Python 3. Run python3 Hangman.py in a terminal if you have Python 3.

null 1 Dec 24, 2022
Recreation of HexGame in Pygame. More features will come soon !

Hex with Pygame Historical point of view What Are the rules of this game ? Some Strategies and tips The algorithm for the Win Other fonctionnalities W

null 4 Mar 26, 2022
null 3 Oct 22, 2021
Made with pygame. Multiplayer game using socket module and threading.

Rock Paper Scissor made with python-pygame. Poorly made, as a beginner in programming. Multiplayer with server code and client code provided.

AllenJo 1 Dec 29, 2021
Inflitator is a classic doom and wolfenstein3D like game made in Python, using the famous PYGAME module.

INFLITATOR Raycaster INFLITATOR is a raycaster made in Python3 with Pygame. It is a game built on top of a simple engine of the same name. An example

Zanvok Corporation 1 Jan 7, 2022
Wordle-Python - A simple low-key clone of the popular game WORDLE made with python and a 2D Graphics module Pygame

Wordle-Python A simple low-key clone of the popular game WORDLE made with python

Showmick Kar 7 Feb 10, 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 menu for pygame. Simple, and easy to use

pygame-menu Source repo on GitHub, and run it on Repl.it Introduction Pygame-menu is a python-pygame library for creating menus and GUIs. It supports

Pablo Pizarro R. 411 Dec 27, 2022
A small, Pygame-based library project intended for personal use.

EzyGame Version 0.0.1 A simple library project intended for personal use with Pygame. Warning: I am a very amateur programmer, so the code will probab

Dorbell 1 Jan 8, 2022
Python desktop application to create, distribute, discover, and run codegames

Python desktop application to create, distribute, discover, and run codegames

null 2 Nov 16, 2021
Playing memory game is fun and the more harder it is the more challenging it is.

Playing memory game is fun and the more harder it is the more challenging it is. Playing thi sgame make us stress free and also happy. So, I have decided to make a memory Game which people can play while doing work. To pass your time and to be little happy, play this wonderful memory game - **JACKPOT** while doing your work and sitting in front of your computer.

Shreejan Dolai 3 Nov 11, 2022
learn and have fun developing 2D retro games using python and pygame

Retro 2D Game Development Using Python + PyGame Skill up your programming skills with a walk down the memory lane. Learn how to create a retro 2D game

Marvin Trilles 1 Feb 23, 2022
Quiz game made entirely with python and pygame for school work

Tabela de conteúdo Descrição Como instalar Linguagens usadas Contribuidores Créditos Problemas com o jogo? Contate-nos Descrição Quiz feito inteiramen

null 3 Apr 12, 2022
FlappyBird game with python and pygame

FlappyBird game with python and pygame

Mohammad Dori 4 Jul 15, 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 1, 2021
This is a 2D Link to the Past-esque game made using Python 3.2.5 and pygame 1.9.2

Queen-s-Demise Queen's Demise This is a 2D Link to the Past-esque game made using Python 3.2.5 and pygame 1.9.2 I made this for a game development cla

Zoey 1 Dec 15, 2021
pygame is a Free and Open Source python programming language library for making multimedia applications like games built on top of the excellent SDL library. C, Python, Native, OpenGL.

pygame is a Free and Open Source python programming language library for making multimedia applications like games built on top of the excellent SDL library. C, Python, Native, OpenGL.

pygame 5.6k Jan 1, 2023