PYGLET: acceso a la propiedad de color de fondo de selección del diseño de texto incremental

En este artículo, veremos cómo podemos acceder al color de fondo de selección del objeto de diseño de texto incremental 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). El diseño de texto incremental muestra texto que es adecuado para la edición interactiva y/o el desplazamiento de documentos grandes. A diferencia de TextLayout y ScrollableTextLayout, esta clase genera listas de vértices solo para las líneas de texto que son visibles. A medida que se desplaza el documento, las listas de vértices se eliminan y se crean según corresponda para mantener el uso de la memoria de video al mínimo y mejorar la velocidad de procesamiento.

Podemos crear una ventana y un diseño de texto incremental con la ayuda de los comandos que se indican a continuación. 

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

# creating a incremental layout
layout = pyglet.text.layout.IncrementalTextLayout(document, width, height)

Para crear una ventana, usamos el atributo selection_background_color con el objeto de diseño
Sintaxis: layout.selection_background_color
Argumento: no requiere argumento
Retorno: devuelve una tupla 
 

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 = "GeeksforGeeks Learn and Grow, Portal for Geeks"
  
# batch object
batch = pyglet.graphics.Batch()
  
# creating a formatted document
# unlike unformatted document it is formatted
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, multiline = True, 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"))
 
# layout update event
@window.event
def on_layout_update():
     
    # printing text
    print("Layout update Event")
  
# 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 = pyglet.resource.image("gfg.png")
 
# setting image as icon
window.set_icon(img)
 
# accessing selection background color
# assigning new color
layout.selection_background_color = (100, 255, 100, 255)
 
 
# setting selection range
layout.set_selection(10, 30)
 
 
  
# 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 *