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.
The DOS game from the 80s re-written in Python from Scratch!

Drugwars The DOS game from the 80s re-written in Python from Scratch! Play in your browser Here Installation Recommended: Using pip pip3 install drugw

Max Bridgland 45 Jan 03, 2023
MinMax Algo , Python

Write a PYTHON program to play the game of TIC-TAC-TOE on a 3×3 board with alternate inputs from user and computer.

Naman Anand 1 Nov 26, 2021
Python game engine for 2D multiplayer online games.

LAN-Caster The goal of LAN-Caster is to provide an easy-to-use code base (game engine) for developing 2D multiplayer online games. LAN-Caster original

Douglas Bakewell 1 Feb 11, 2022
An automation bot to play Myuu Discord game

Auto selfbot Myuu is a self Discordbot, meaning it will use your TOKEN to logged as your account and take commands from yourself to play the game.

6 Dec 15, 2022
linorobot2 is a ROS2 port of the linorobot package

linorobot2 is a ROS2 port of the linorobot package. If you're planning to build your own custom ROS2 robot (2WD, 4WD, Mecanum Drive) using accessible parts, then this package is for you. This reposit

linorobot 195 Dec 29, 2022
Mandaw 2 Mar 01, 2022
🌍🍓 A better MCPi Launcher

Planet Launcher A better, maintained launcher for the Minecraft: Pi Edition Reborn mod. Report Bug | Request Feature Planet is a maintained, feature-r

15 Oct 19, 2022
Rock-Paper-Scissors - Rock Paper Scissors With Python

Rock-Paper-Scissors The familiar game of Rock, Paper, Scissors is played like th

Lateefah Ajadi 0 Jan 15, 2022
BritishTrainsDepartureBoard - A pygame program that immitates the dot matrix departure screens found at National Rail stations

BritishTrainsDepartureBoard - A pygame program that immitates the dot matrix departure screens found at National Rail stations

Finn O'Neill 3 Aug 10, 2022
Vac-Man in Python

Vac-Man in Python This is my personal version of Vax-man game using python, which is the first task of EA Software Engineering Virtual Experience Prog

ZiXiang Luo 3 Jan 05, 2022
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
Minecraft Bedrock Server Control GUI

A control dashboard to monitor and control your minecraft bedrock dedicated server through an easy user interface. Created by Nathan-Busse 13 January 2022 Made with Python 3.8

Nathan Busse 3 Dec 11, 2022
Repository for the DecodED2 Game Project!

DecodED2 Game Project Hello everyone! Welcome to the GitHub Repository for DecodED2, as a start you'll need to clone this repository and make sure you

6 Sep 29, 2021
Inject custom C++ code into GameMaker Studio 2 YYC builds

YYC Boost Inject custom C++ code into GameMaker Studio 2 YYC builds! WARNING: This tool is currently in an early stage of development and it is not gu

Patrik Kraif 7 Dec 30, 2022
To be easier to create backup files for the game StoneShard

StoneShard_save_backup_tool A tool to create backups and load them. Setup Configure the program by opening the "config.json" file. In this file you wi

Lucas V. Moog Brentano 6 Sep 15, 2022
The Original Snake Game. Maneuver a snake in its burrow and earn points while avoiding the snake itself and the walls of the snake burrow.

Maneuver a snake in its burrow and earn points while avoiding the snake itself and the walls of the snake burrow. The snake grows when it eats an apple by default which can be disabled in the setting

17 Nov 12, 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

3 Apr 12, 2022
TetrisAI - Tetris AI Bot using computer vision to play game automatically

Tetris AI Tetris AI Bot using computer vision to play game automatically bot.py

11 Aug 29, 2022
A comprehensive, feature-rich, open source, and portable, collection of Solitaire games.

PySol Fan Club edition This is an open source and portable (Windows, Linux and Mac OS X) collection of Card Solitaire/Patience games written in Python

Shlomi Fish 368 Dec 28, 2022
Space Invaders x Asteroid Game

Retro Journey 1: Space Invaders A simple implementation of a retro style video game where users compete against asteroids and the goal is to destroy a

Sandesh Lamsal 2 Aug 05, 2022