PYGLET – Eliminación de texto de un documento formateado

En este artículo, veremos cómo podemos eliminar texto del documento formateado en el módulo PYGLET en python. Pyglet es una biblioteca fácil de usar pero poderosa para desarrollar aplicaciones GUI visualmente ricas como juegos, multimedia, etc. Una ventana es un objeto «pesado» que ocupa los recursos del sistema operativo. Las ventanas pueden aparecer como regiones flotantes o pueden configurarse para llenar una pantalla completa (pantalla completa). Los documentos formateados generalmente se presentan para mejorar su legibilidad humana agregando estructura, como encabezados, sangría, variaciones de fuente y otros dispositivos que transforman el texto sin formato (sin formato) para que tengan una apariencia similar a los trabajos publicados. El usuario puede eliminar o insertar texto en cualquier momento, aunque a veces es necesario eliminar el texto mediante programación.

Podemos crear una ventana y un documento formateado con la ayuda de los comandos que se indican a continuación. 

# creating a window
window = pyglet.window.Window(width, height, title)

# creating document
document = pyglet.text.document.FormattedDocument(text)

Para crear una ventana, usamos el método delete_text con el objeto del documento
Sintaxis: document.delete_text (inicio, fin)
Argumento: toma dos enteros como argumento
Retorno: devuelve Ninguno 
 

A continuación se muestra la implementación.  

Python3

# importing pyglet module
import pyglet
import pyglet.window.key
 
# width of window
width = 500
 
# height of window
height = 500
 
# caption i.e title of the window
title = "Geeksforgeeks"
 
# creating a window
window = pyglet.window.Window(width, height, title)
 
# text
text = "Welcome to GeeksforGeeks Have a nice day"
 
# batch object
batch = pyglet.graphics.Batch()
 
# creating a formatted document
document = pyglet.text.document.FormattedDocument(text)
 
# setting style to the document
document.set_style(0, len(document.text), dict(font_name ='Arial', font_size = 16, color =(255, 255, 255, 255)))
 
# creating a incremental text layout
layout = pyglet.text.layout.IncrementalTextLayout(document, 400, 350, batch = batch)
 
# creating a caret
caret = pyglet.text.caret.Caret(layout, color =(150, 255, 150))
 
 
 
# caret to window push handlers
window.push_handlers(caret)
 
# setting caret style
caret.set_style(dict(font_name ="Arial"))
 
 
# on draw event
@window.event
def on_draw():
     
    # clear the window
    window.clear()
     
    # draw the batch
    batch.draw()
     
    # caret to window push handlers
    window.push_handlers(caret)
 
     
# key press event   
@window.event
def on_key_press(symbol, modifier):
 
    # key "C" get press
    if symbol == pyglet.window.key.C:
         
        # closing the window
        # window.close()
        pass
     
 
# image for icon
img = image = pyglet.resource.image("logo.png")
 
# setting image as icon
window.set_icon(img)
 
# deleting text from the document
document.delete_text(5, 15)
 
 
# start running the application
pyglet.app.run()

Producción : 

Publicación traducida automáticamente

Artículo escrito por rakshitarora y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *