Skip to main content

A WYSIWYG-texteditor based on pygame.

Project description

A WYSIWYG-texteditor based on pygame for pygame

PyPI GitHub

Usage

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

The texteditor takes 5 obligatory parameters and 3 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
  • textAreaWidth : integer - the width of texteditor
  • textAreaHeight : integer - the height of texteditor
  • screen : pygame display surface - on which the texteditor is to be displayed
Optional Parameters with default values
  • line_numbers_flag - a boolean enabling showing line numbers

    Default: False

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

    Default: 'dark'

  • syntax_highlighting_flag - a boolean enabling syntax highlighting for Python code

    Default: False

Setup

Minimal pygame setup
import pygame
from pygame_texteditor import TextEditor

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
# 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
textAreaHeight = 500
textAreaWidth = 800


# Instantiation
TX = TextEditor(offset_X, offset_Y, textAreaWidth, textAreaHeight, 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

Font size

Font size can be customized with the command set_font_size(size) - the parameter is an integer with the default value 12 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

  • codingBackgroundColor
  • codingScrollBarBackgroundColor
  • lineNumberColor
  • lineNumberBackgroundColor
  • textColor

Syntax colors

  • textColor_normal
  • textColor_comments
  • textColor_quotes
  • textColor_operators
  • textColor_keywords
  • textColor_function
  • textColor_builtin

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.6.3.tar.gz (50.9 kB view details)

Uploaded Source

Built Distribution

pygame_texteditor-0.6.3-py3-none-any.whl (54.1 kB view details)

Uploaded Python 3

File details

Details for the file pygame-texteditor-0.6.3.tar.gz.

File metadata

  • Download URL: pygame-texteditor-0.6.3.tar.gz
  • Upload date:
  • Size: 50.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for pygame-texteditor-0.6.3.tar.gz
Algorithm Hash digest
SHA256 9118a86c164e74986f72fe039ccf77d82e9b8a92e3b03ec458a3cf925d80fdb2
MD5 ea0385088b419f86c3ef143210d0b65e
BLAKE2b-256 65c0dd17e6837ebf0a9630762b2b3fd226016f86a776fcbc571d63e4cca1aaaa

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pygame_texteditor-0.6.3-py3-none-any.whl
  • Upload date:
  • Size: 54.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for pygame_texteditor-0.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4455e8b2562ce8a74536ea1e31c6186839bc06061e162311ee3cae05b93552e7
MD5 4faba4880793517d08a2a2c856bc429e
BLAKE2b-256 78225ab2d87040d2b13fd2a779c337b1ad51993ba182e415d5719e33fadca2c4

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