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.
Tic Tac Toe Python Game GUI

Tic Tac Toe is one of the most played games and is the best time killer game that you can play anywhere with just a pen and paper.

Astitva Veer Garg 1 Jan 11, 2022
Termordle - a terminal based wordle clone in python

Termordle - a terminal based wordle clone in python

2 Feb 08, 2022
Simple wordle clone + solver + backtesting

Wordle clone + solver + backtesting I created something. Or rather, I found about this game last week and decided that one challenge a day wasn't goin

1 Feb 08, 2022
Chess Game using Python

Chess Game is a single-player game where the objective is same as the original chess game. You just need to place your chess piece in a correct position. The purpose of the system is to provide some

Yogesh Selvarajan 1 Aug 15, 2022
Multiple hacks that breaks the game

Blooket-Hack All of the cheats are based on a game mode.

glizzz_y 484 Feb 25, 2022
An interactive pygame implementation of quadtree spatial quantization

QuadTree-py An interactive pygame implementation of quadtree spatial quantization Contents Installation Usage API Reference TODO Installation Clone th

Ethan 1 Dec 05, 2021
Quiz Game: answering questions naturally with a friendly UI to enjoy the game

About Quiz Game : The Game is about answering questions naturally with a friendl

4 Jan 19, 2022
Pratice Project - Tic tac toe game

Hello! This tic-tac-toe game project and its notes are result from a course pratice milestone. The project itself is written in Python using the Jupyt

Rafael Nascimento 1 Jan 07, 2022
Email guesser - Guessing BF email based on emailGuesser by WhiteHatInspector

email_guesser Guessing BF email based on emailGuesser by WhiteHatInspector (http

4 Dec 25, 2022
A Python based program that displays Your Minecraft Server's Status Infos.

Minecraft-server-Status This (very) small python script allows you to view any Minecraft server's status Information Usage Download the file, install

Jonas_Jones 2 Oct 05, 2022
Tic-Tac-Toe - Tic-Tac-Toe game build With Python

Tic Tac Toe This game is very popular amongst all of us and even fun to build as

PyLaboratory 0 Feb 06, 2022
KBYD - Simple Bulls and Cows Game

KBYD KBYD - Simple Bulls and Cows Game How to Play KBYD is a simple Bulls and Cows Game. When the game starts, the computer randomly generates 3 to 5

1 Dec 04, 2021
A fully automated system that transforms Twitch clips into gaming compilations

A fully automated system that transforms Twitch clips into gaming compilations Authors: Christian C., Moritz M., Luca S. Related Projects: Neural Netw

215 Dec 27, 2022
A two-player strategy game played on a rectangular grid made up of smaller square cells of chocolate 🍫 or cookies 🍪

Chomp Game ©️ Chomp is a two-player strategy game played on a rectangular grid made up of smaller square cells of chocolate 🍫 or cookies 🍪 , which c

Farivar Tabatabaei 2 Feb 02, 2022
Abandoned plan for a clone of the old Flash game Star Relic

space-grid When I was in middle school, I was a fan of the Flash game Star Relic (no longer playable in modern browsers, but it works alright in Flash

Radon Rosborough 3 Aug 23, 2021
A tool to design a planet for Galaxy Life Reborn game.

GLRBaseDesigner A program to design your planet for Galaxy Life Reborn game. Description Do you want to share your base design with friends? Now it's

jjay31 9 Dec 16, 2022
🍦 Cheat for cs:go written in Python.

Cs::Fuck 🍦 Cheat for cs:go written in Python. You can show a video here: https://vimeo.com/642730650 Feature. TriggerBot Glow Esp NoFlash Setup. 0. p

Ѵιcнч 10 Sep 23, 2022
Open source Brawl Stars server emulator for version 29 of the game!

Welcome to Classic-Brawl v29 Remake 👋 Open source Brawl Stars server emulator for version 29 of the game! (Remake) What's working ? Battles Trophies

CrossFire 4 Jan 19, 2022
Simple darts game using Tkinter and sqlite3. Also associated with Python.

Ever wanted to play a simple and fun game before, and it even keeps a database of your score? Well here it is!! Introducing; Darts! A simple and fun g

an aspirin 2 Dec 19, 2021
A project to san the internet of all open Minecraft servers.

MC-Server-Finder A project that scans the internet to find open Minecraft servers. Install the dependencies by running pip install -r requirements.txt

drakeerv 8 Mar 12, 2022