Skip to main content

A WYSIWYG-texteditor based on pygame.

Project description

A WYSIWYG-texteditor based on pygame for pygame

PyPI GitHub

Introduction & examples

The text editor can be inserted into any existing pygame window. A minimal code example of it being activated within an existing pygame window can be found below.

The code editor comes with line numbers and syntax highlighting for python if enabled:

Example with default configuration:

Usage

The texteditor takes 5 obligatory parameters and 4 optional parameters.

Obligatory parameters
  • offset_X : integer - the offset from the left border of the pygame screen
  • offset_y : integer - the offset from the top border of the pygame screen
  • editor_width : integer - the width of texteditor
  • editor_height : integer - the height of texteditor
  • screen : pygame display surface - on which the texteditor is to be displayed
Optional Parameters with default values
  • display_line_numbers - a boolean enabling showing line numbers

    Default: False

  • style - a String setting the color scheme of editor and syntax highlighting

    Default: 'dark'

  • syntax_highlighting_python - a boolean enabling syntax highlighting for Python code

    Default: False

  • font_size - an integer to set for the font size.

    Default: 16

Setup

Minimal pygame setup
import pygame

pygame.init()
screenHeight = 600
screenWidth = 900
screen = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("Pygame")
pygame.display.get_surface().fill((200, 200, 200))  # background coloring
Minimal texteditor setup
import pygame
from pygame_texteditor import TextEditor

# parameters
screen = pygame.display.get_surface()  # get existing pygame window/screen
offset_X = 50  # offset from the left border of the pygame window
offset_Y = 50  # offset from the top border of the pygame window
editor_height = 800
editor_width = 500


# Instantiation
TX = TextEditor(offset_X, offset_Y, editor_width, editor_height, screen)
TX.set_line_numbers(True)  # optional
TX.set_syntax_highlighting(True)  # optional

while True:  # pygame-loop
    # capture input
    pygame_events = pygame.event.get()
    pressed_keys = pygame.key.get_pressed()
    mouse_x, mouse_y = pygame.mouse.get_pos()
    mouse_pressed = pygame.mouse.get_pressed()

    # display editor functionality once per loop
    TX.display_editor(pygame_events, pressed_keys, mouse_x, mouse_y, mouse_pressed)

    # update pygame window
    pygame.display.flip()
Retrieving text from the editor

The editor offers the function get_text_as_string() to retrieve the entire text as a String from the editor. Lines are separated by the new line character \n.

The editor offers the function get_text_as_list() to retrieve the entire text as a list from the editor. Each String-item in the list represents one line from the editor.

Removing text from the editor

The editor offers the function clear_text() to clear the editor of any text.

Inserting text into the editor

Inserting text can be done by using one of the two available functions:

  1. With a list of strings in which each string represents one line, or
  2. With a string which includes linebreak characters which get parsed.
set_text_from_list(["First line", "Second Line.", "Third Line."]
set_text_from_string("First line.\nSecond line.\nThird Line")

Customization

Cursor mode

Cursor mode can either be static or blinking (=default).

TX = TextEditor(...)
TX.set_cursor_mode("static")
TX.set_cursor_mode("blinking")

Key repetition speeds

While a key is being held, multiple key events are being triggered. The delay of the first repetition as well as the interval between all sequential key triggers can be customized by using the function set_key_repetition(delay=300, intervall=30).

From the official documentation:

The delay parameter is the number of milliseconds before the first repeated pygame.KEYDOWN event will be sent. After that, another pygame.KEYDOWN event will be sent every interval milliseconds.

Font Customization

The editor uses a ttf file to set the font for the editor. By default, the Courier monospace font is used.

A custom font can be loaded with the following method, passing an absolute path:

  • set_font_from_ttf("X:\path\to\custom\font.ttf")

DISCLAIMER: As the width of a letter (space) is only calculated once after setting the font_size, any fonts that are not monospace will lead to the editor not working correctly anymore, as it cannot be determined correctly between which letters the user clicked.

Font size

Font size can be customized with the command set_font_size(size) - the parameter is an integer with the default value 16 to be able to reset it.

Line Numbers

Line numbers can be shown on the left side of the editor. Line numbers begin with 0 as is the Pythonian way.

Line numbers can be enabled and disabled with set_line_numbers(Boolean).

Syntax Highlighting

The editor comes with syntax highlighting for Python code. Tokenization is based on the pygment package.

Syntax highlighting can be enabled/disabled with set_syntax_coloring(boolean_value).

The syntax colors being used are also specified in the yml style file.

Color-scheme customization

The editor uses a yml file to set the color-scheme for the editor itself and for the syntax coloring.

Two styles are delivered with the editor, they can be activated respectively by:

  • set_colorscheme("dark")
  • set_colorscheme("bright")

A custom style can be loaded with the following method from a created yml file:

  • set_colorscheme_from_yaml("X:\path\to\custom\filename.yml")

All keys must be present with values. Acceptable values are RGB colors in the following format: (255, 255, 255) or 255, 255, 255.

The following keys are required in the stylename.yml file, syntax colors are only used if syntax highlighting is enabled, but are still required to be included.

Editor colors (source: bright.yml)

  • codingBackgroundColor: (255, 255, 255)
  • codingScrollBarBackgroundColor: (49, 50, 50)
  • lineNumberColor: (255, 255, 255)
  • lineNumberBackgroundColor: (60, 61, 61)
  • textColor: (255, 255, 255)

** Syntax colors** (source: bright.yml)

  • textColor_normal: (0, 255, 255)
  • textColor_comments: (119, 115, 115)
  • textColor_quotes: (227, 215, 115)
  • textColor_operators: (237, 36, 36)
  • textColor_keywords: (237, 36, 36)
  • textColor_function: (50, 150, 36)
  • textColor_builtin: (50, 50, 136)

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

pygame_texteditor-0.7.0.tar.gz (53.8 kB view details)

Uploaded Source

Built Distribution

pygame_texteditor-0.7.0-py3-none-any.whl (56.0 kB view details)

Uploaded Python 3

File details

Details for the file pygame_texteditor-0.7.0.tar.gz.

File metadata

  • Download URL: pygame_texteditor-0.7.0.tar.gz
  • Upload date:
  • Size: 53.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.8.10 Windows/10

File hashes

Hashes for pygame_texteditor-0.7.0.tar.gz
Algorithm Hash digest
SHA256 6d30b3949a68e666dd17aea8cc99eca5aa08b4d6bfa2ca24e3d1e1975c9b82fe
MD5 653fa1e4c978c281638cf6b78d8a4c3a
BLAKE2b-256 a7be78b22abcc097b9de61b9aa370b47217a1e631cb18a1879adb07d7415f0e3

See more details on using hashes here.

File details

Details for the file pygame_texteditor-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pygame_texteditor-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 573b56c92b9fcf293870d86fd237168becfbf1040cbd5bbac59ef29bad895b10
MD5 300a00374b45700a59256e5dc06e342e
BLAKE2b-256 6ed3ac81e535edc05b08fb25f398f4c6deba313b571119375f4eaf8e744ab934

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