You can download this code by clicking the button below.
This code is now available for download.
This code defines a Pyglet window where users can input text. Pressing the space key clears the text, pressing the left arrow key deletes the last character, pressing the right arrow key adds a space at the end of the text, and pressing the delete key clears the text.
Technology Stack : Pyglet
Code Type : Python Function
Code Difficulty : Intermediate
import pyglet
from pyglet.window import key
def random_text_input(window):
@window.event
def on_key_press(symbol, modifiers):
if symbol == key.SPACE:
window.clear()
window.text = " "
elif symbol == key.LEFT:
window.text = window.text[:-1]
elif symbol == key.RIGHT:
window.text += " "
elif symbol == key.DELETE:
window.clear()
else:
window.text += pyglet.window.key.name(symbol)
def main():
window = pyglet.window.Window(width=400, height=200)
window.text = ""
label = pyglet.text.Label(window.text, font_name='Arial', font_size=24,
x=10, y=window.height // 2,
anchor_x='left', anchor_y='center')
window.push_handlers(random_text_input(window))
pyglet.app.run()
if __name__ == '__main__':
main()