Skip to main content

A UIKit for Pygame

Project description

Contributors Issues License Publish to PyPI

img.png

Getting Started

pip install PygameUIKit

Easy to use

import pygame
from PygameUIKit import Group, button, slider

W = 800
H = 600


class MyGame:
    def __init__(self):
        # You class code here
        self.window = pygame.display.set_mode((W, H), pygame.RESIZABLE)
        # Ui
        self.ui = Group()  # Create a group to hold all the ui elements. This is filled with the ui elements below thanks to the ui_group parameter
        self.btn_start = button.ButtonText("Start",
                                           self.start,
                                           rect_color=(85, 145, 92),
                                           fixed_width=200,
                                           border_radius=10,
                                           text_align="center",
                                           ui_group=self.ui)

        self.btn_quit = button.ButtonText("Quit", self.quit,
                                          rect_color=(181, 71, 71),
                                          fixed_width=180,
                                          border_radius=10,
                                          text_align="center",
                                          ui_group=self.ui)

    def run(self):
        while True:  # Game loop
            # Your game code here

            # Ui
            # Handle events
            events = pygame.event.get()
            for event in events:
                if event.type == pygame.QUIT:
                    pygame.quit()
                self.ui.handle_event(event)  # Handle the events for all the ui elements

            # Draw
            self.window.fill((0, 0, 0))
            self.btn_start.draw(self.window, *self.btn_start.surface.get_rect(center=(W // 2, H // 2 - 50)).topleft)
            self.btn_quit.draw(self.window, *self.btn_quit.surface.get_rect(center=(W // 2, H // 2 + 50)).topleft)
            pygame.display.flip()

    def start(self):
        print("Starting")

    def quit(self):
        print("Quitting")
        pygame.quit()


if __name__ == "__main__":
    pygame.init()
    MyGame().run()

Just put all your ui elements in a group and call group.handle_event(event). You will have to draw the elements yourself with element.draw(win, x, y).

That is because you might want to have a custom layout where the x and y coordinates are calculated on the fly.

Available elements

  • Buttons
    • button.ButtonText
    • button.ButtonImage
    • button.ButtonImageText
    • button.ButtonPngIcon
    • button.ButtonTwoStates
    • button.ButtonThreadText
  • Slider
  • ComboBox
  • TextInput
  • Label
  • BarChart

And more to come :)

Contributing

Feel free to do a pull request if you want to add a new element or improve the code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PygameUIKit-0.1.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

PygameUIKit-0.1.0-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file PygameUIKit-0.1.0.tar.gz.

File metadata

  • Download URL: PygameUIKit-0.1.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for PygameUIKit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3275cc659b311af84bc1899721f3dc4d94f58f7f056695a0d6be86842779b66c
MD5 0be9a48dcab060594f03e0e526d0959b
BLAKE2b-256 2947e1436b64a3da52afead58d5757430cc88c9e1d782729c8d4099e572bb0b4

See more details on using hashes here.

File details

Details for the file PygameUIKit-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: PygameUIKit-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for PygameUIKit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e881f43c8a21de7312944930bd7ec7ba3bda56e85e52b8adb7e105e1d47abd7c
MD5 b6d0001d9a8ad2b27fffe1fd74c61d2d
BLAKE2b-256 9a496c5336d0a813303bd958f200db3f3f6b19dff934b474650640b21317cf7c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page