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.
This repository has the lessons of the gamming programming course

learning-python-game-programming This repository has the lessons of the gamming programming course Na faculdade, estou fazendo a disciplina de program

Mateus Faustino 1 Nov 16, 2021
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
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
Just a simple Tic Tac Toe game, built with Python

TicTacToe Author: Gabriel Lima Table of Contents About Getting Started Linux Windows About This is one of the first projects I built when I first star

1 Nov 28, 2021
BUG OUTBREAK is a game of adventure and shooting.

BUG OUTBREAK BUG OUTBREAK is a game of adventure and shooting. I am building the game for Github Game Off 2021. This game has 5 levels. You have to co

Shreejan Dolai 3 Nov 11, 2022
A "guess the number" game on a GUI interface using Tkinter library🙂

A "guess the number" game on a GUI interface using Tkinter library🙂

Arsalan 2 Feb 01, 2022
A simple yet powerful Snake Game made with myPygameWorkflow

snakeGame A simple yet powerful Snake Game made with myPygameWorkflow. Requirments python3 Python.org myPygameWorkflow Github Ripo Usage $ cd main $ p

DuskyElf 1 Dec 26, 2021
Jogo da velha escrito em python para 1 ou 2 jogadores

O Jogo da Velha Esse jogo da velha foi desenvolvido por mim em python, como um desafio de programar um jogo da velha em menos de 24 horas, no qual o c

Gabriel Castelo Branco 5 Jun 18, 2021
Projeto Flappy Bird temática doom, projeto python e pygame

Doom-Bird Tecnologias usadas Requisitos para inicializar o jogo: Python faça o download em: https://www.python.org/downloads/ Após instalar o Python d

João Guilherme 1 Dec 08, 2021
Chesston (Chess+Python) is a two-player chess game with graphical user interface written in PyQt5

♟️ Chesston (Chess+Python) is a two-player chess game with graphical user interface written in PyQt5. 💿 Dependencies This program uses Py

6 May 26, 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
Code for an arcade pop-a-shot style basketball game on Raspberry Pi

Basketball-Game Code for an arcade pop-a-shot style basketball game on Raspberry Pi, made over the course of winter break 2022. How To Run: Running th

Seth Reis 1 Jan 21, 2022
Minesweeper clone with 3 modes of difficulty, UI scaling and custom games feature.

Insect Sweeper Minesweeper clone with 3 modes of difficulty, UI scaling and custom games feature. Mines are replaced with random insects that a player

Piotr Data 1 Nov 05, 2021
This is a basic virtual quiz game using opencv-python

Basic Virtual-Quiz-Game This is a basic structure of a virtual quiz game using opencv-python. As the camera window opens up we can see the questions a

2 Dec 11, 2021
Deep Running

Deep Running 1. Install $ pip install --user deep_running 2. Lap N Lap. Name Remarks Citation Meta 1 Mario Deeeeeep Running I was born to run. dannyso

karaage 69 Jan 31, 2022
An interactive pygame implementation of Conway's Game of Life

Game of Life An interactive pygame implementation of Conway's Game of Life Installation Clone the repo and navigate into it. git clone https://github.

Ethan 1 Dec 05, 2021
This is a simple Tic Tac Toe game built with Python.

This is a simple Tic Tac Toe game built with Python.

⚝αикιт кυмαя⚝ 6 Sep 01, 2022
A Python Sudoku Game Made with Pygame.

A Python Sudoku Game Made with Pygame. A Begginer Aimed at Learning Git, This Game Uses a Puzzle Generator Made by RutledgePaulV, Link to his Repo:

helaxious 3 Jun 29, 2022
Easily manage wine prefixes in a new way. Run Windows software and games on Linux

Bottles Easily manage wineprefix using environments Documentation · Forums · Telegram group · Funding 📚 Documentation Before opening a new issue, che

Bottles 4.1k Jan 09, 2023
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