是否有一种相对简单的方法从用户获取文本输入并将其存储在变量中? 例如,通过textinput请求名称并将其显示在标签上。
我检查了pyglet text_input.py示例,但它只演示了文本的显示,它没有从表单中获取值。 任何示例代码都会有很大帮助。感谢。
答案 0 :(得分:0)
我自己正在玩pyglet text_input.py示例,我发现了如何打印文本。因为它存储在我使用的小部件中:
print(self.widgets[0].document.text)
然后可以将此文本轻松存储在变量中:
my_text = self.widgets[0].document.text
print(my_text)
希望这会对你有所帮助(尽管答案是迟了一年)。
答案 1 :(得分:0)
感谢您的回复,我在此期间发现,也忘了这个帖子,但这里是我用过的代码。
按“&t”键开始输入输入。然后标签会在每次按键时更新。 Backspace通过删除最后一个字符来完成它的工作,而Enter存储该值。
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