1
\$\begingroup\$

I have been trying to make a simple game, and have recently been trying to work on a menu system for it. I am trying to insert a TextEntry box using the examples they have provided themselves.

I have attempted to replicate their code, however I cannot enter any text with the TextEntry. The other widgets work fine, it is just this one. I am unsure if I need to call it into focus, but there does not appear to be any way to do so.

The relevant code is as such:

import pyglet


window = pyglet.window.Window(540, 500, caption="Widget Example")
batch = pyglet.graphics.Batch()
pyglet.gl.glClearColor(0.8, 0.8, 0.8, 1.0)


@window.event
def on_draw():
    window.clear()
    batch.draw()


def text_entry_handler(text):
    text_entry_label.text = f"Text: {text}"

frame = pyglet.gui.Frame(window, order=4)

text_entry = pyglet.gui.TextEntry("Enter Your Name", 100, 100, 150, batch=batch)
frame.add_widget(text_entry)
text_entry.set_handler('on_commit', text_entry_handler)
text_entry_label = pyglet.text.Label("Text: None", x=300, y=100, batch=batch, color=(0, 0, 0, 255))

pyglet.app.run()

Any help would be appreciated.

\$\endgroup\$
3
  • \$\begingroup\$ Can you input text when the mouse is above the text entry? When the mouse is moved away from the text entry it wil stop accept input? \$\endgroup\$ Commented Oct 25, 2023 at 5:20
  • \$\begingroup\$ Your code seems to work fine for me. I can highlight single or all words with dbl- or triple-click, can delete with Bkspc or Del, etc. There are definitely some quirks though, where things just stop working until you use the mouse. I wonder what version of pyglet you were using, and whether there have been some updates/fixes since then? \$\endgroup\$ Commented Feb 3 at 5:34
  • \$\begingroup\$ Thanks for your question. I had no idea Frames were a thing. Even searching Google for "pyglet frame" only turns up stuff about frame rates. \$\endgroup\$ Commented Feb 3 at 5:52

1 Answer 1

0
\$\begingroup\$

For me, text entry improved when I added a line to the on_draw method to set focus to the TextEntry object every time the event is called.

import pyglet
window = pyglet.window.Window(width=800, height=600, caption='Input Testing')
batch = pyglet.graphics.Batch()
pyglet.gl.glClearColor(0.8,0.8,0.8,1.0)

@window.event
def on_draw():
    window.clear()
    batch.draw()
    text_entry.focus = True # set focus to the text entry every time the screen is redrawn.  

def text_entry_handler(text):
    text_entry_label.text = f"Text: {text}"

frame = pyglet.gui.Frame(window, order=4)

text_entry = pyglet.gui.TextEntry("Enter Your Name", 100, 100, 150, batch=batch)
text_entry.set_handler('on_commit', text_entry_handler) 

frame.add_widget(text_entry)
text_entry_label = pyglet.text.Label("Text: None", 300, 100, batch=batch)
pyglet.app.run()

I learned a lot reading and fiddling with this code, ended up answering my own question and writing a method that cleared the name box whenever new text was entered into it (which I'll probably need myself one day). I doubt, a year later, you still need help with this, but I'm glad I read it at least.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .