是否有一種相對簡單的方式從用戶獲取文本輸入並將其存儲在變量中? 通過textinput詢問名稱並將其顯示在標籤上。Pyglet - 文本輸入 - 如何獲取它的值變量?
我檢出了pyglet的text_input.py示例,但它只演示文本的顯示,它並不從窗體中獲取值。 任何示例代碼都會有所幫助。謝謝。
是否有一種相對簡單的方式從用戶獲取文本輸入並將其存儲在變量中? 通過textinput詢問名稱並將其顯示在標籤上。Pyglet - 文本輸入 - 如何獲取它的值變量?
我檢出了pyglet的text_input.py示例,但它只演示文本的顯示,它並不從窗體中獲取值。 任何示例代碼都會有所幫助。謝謝。
我只是在玩pyglet的text_input.py例子,我發現瞭如何打印文本。因爲它是存儲在一個widget我用:
print(self.widgets[0].document.text)
可以將此文本然後輕鬆地存儲在一個變量:
my_text = self.widgets[0].document.text
print(my_text)
希望這有助於你一點(雖然答案是晚了一年:)
感謝您的回覆,我發現在此期間,也忘記了這個線程,但這裏是我用過的代碼。
按'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