Skip to main content

Provides a chess board as a kivy widget.

Project description

ChessBoard - a Kivy Widget to interact with a Chess Board

This package provides a chess board as a kivy widget. It allows users to interact with the chess board by dragging and dropping pieces, and it can be integrated into Kivy applications.

The widget displays the current state of a chess.Board from the python-chess library. It supports highlighting specific moves and selecting squares. By design, it does not implement any game logic or enforce chess rules; it simply reflects the state of the provided chess.Board and notifies the host application of user interactions.

Exmaple Usage

See the documentation for more details and full API reference.

import chess
from kivy.app import App
from kivy.config import Config as KivyConfig
from kivy.logger import LOG_LEVELS, Logger
from kivy.utils import platform

from kivy_chess_board import ChessBoard


class MyApp(App):
    def build(self):
        cb = ChessBoard()

        # At any time we can set the board to a specific position and highlight
        # moves.
        cb.update(
            chess.Board(),
            moves_to_be_highlighted=[
                chess.Move.from_uci("e2e4"),
                chess.Move.from_uci("d2d4"),
            ],
            move_selector=0,  # e2e4 move appears "selected"
        )

        cb.process_square_selection = process_square
        cb.process_move_selection = process_move
        return cb


def process_square(cb: ChessBoard, pos: chess.Square) -> None:
    """Callback to process square selections (clicks as well as drag-and-drop)."""
    square_name = chess.square_name(pos)
    Logger.debug(f"App: Square selected: {square_name}")


def process_move(cb: ChessBoard, move: chess.Move) -> None:
    """Callback to process move selection."""
    if move not in cb.board.legal_moves:
        Logger.warning("App: Illegal move")
        return

    cb.board.push(move)
    cb.update()
    cb.moves_to_be_highlighted = [move]  # highlight last move


def adjust_kivy_config() -> None:
    """Remove weird touchpad behavior on Linux.

    This is a bug in Kivy itself, not specific to this progream."""
    if platform != "linux":
        return

    if KivyConfig is None or not KivyConfig.has_section("input"):
        return

    for opt in list(KivyConfig.options("input")):
        if KivyConfig.get("input", opt) == "probesysfs":
            KivyConfig.remove_option("input", opt)


Logger.setLevel(LOG_LEVELS["debug"])
adjust_kivy_config()
MyApp().run()

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

kivy_chess_board-0.1.1.tar.gz (332.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kivy_chess_board-0.1.1-py3-none-any.whl (337.7 kB view details)

Uploaded Python 3

File details

Details for the file kivy_chess_board-0.1.1.tar.gz.

File metadata

  • Download URL: kivy_chess_board-0.1.1.tar.gz
  • Upload date:
  • Size: 332.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for kivy_chess_board-0.1.1.tar.gz
Algorithm Hash digest
SHA256 54282c224fe27bd7f8b6333e723808cedfbbfa734ad6de893781b55cef2bd7ad
MD5 493762d152eb35255ac74717851f608d
BLAKE2b-256 4172c3d3e1961a8ff769a5c8ed451867b34226615645cbe768c57a7f85dd50ff

See more details on using hashes here.

File details

Details for the file kivy_chess_board-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for kivy_chess_board-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a7328ea9a267900a7595c2ced59ba3fcf7f3702a1f0ac7dccc4305b182052377
MD5 daa45d4fb69aaaa7ca282d97ec7d685a
BLAKE2b-256 924a33bf589b9ebd3df8c24586b7845925ee51f39f7aa5202f64654034661383

See more details on using hashes here.

Supported by

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