Is there a relatively simple way of getting text input from the user and store it in variable?
Asking for a name via textinput and displaying it on a label for example.
I checked out the pyglet text_input.py example but it only demonstrates the display of the text, it doesnt get the value from the form.
Any example code would help a lot. Thanks.
I'm just playing with the pyglet text_input.py example myself and I found out how to print the text. As it's stored in a widget I used:
This text can be then stored easily in a variable:
my_text = self.widgets.document.text print(my_text)
Hope this helps you a bit (although the answer is a year late :)
Thanks for the reply, i found out in the meantime, also forgot about this thread but here is the code i've used.
Typing the input starts here by pressing 't'. The label then gets updated on every keypress. Backspace does it's job by removing the last character and Enter stores the value.
class Typein(object): text ='' firstt = True # this serves to avoid the first 't' used to activate the typing, # not to get stored as first character of the text. @staticmethod def on_text(text): if Typein.firstt == True and Typein.text == 't': Typein.text = '' Typein.firstt = False Typein.text += text if Typein.firstt != True: Labels.playername_label.text = Typein.text Control.CurrentPlayer.name = Typein.text @staticmethod def on_key_press(symbol,modifiers): if symbol == key.ENTER: Labels.playername_label.text = Typein.text Typein.text ='' GAME_WINDOW.pop_handlers() Control.handleraltered = False elif symbol == key.BACKSPACE: Typein.text = Typein.text[:-1] Labels.playername_label.text = Typein.text elif symbol: return True