0
我一直在玩kivy庫,並一直試圖爲遊戲hang子手[1]創建一個GUI。我試圖創建一個用戶輸入單個字母的TextField,當它們按下return/enter時,而不是textinput.text給出的常量流時,它會更改按鈕文本僅限。(Kivy/Python)當用戶點擊輸入時,從textinput小部件中存儲文本
我已經看過文檔和其他計算器Q & A的,它似乎解決方案或者on_text_validate事件或更改我的Clock.schedule.interval設置,但我無法弄清楚如何實現它在我的代碼。
我的Python文件:
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
class HangmanBoard(BoxLayout):
word = 'hangman'
empty = ["_"]*len(word)
def update(self, *args):
empty_word = self.ids['labelA']
letter_input = self.ids['labelB']
guess = letter_input.text
empty_word.text = ' '.join(self.empty)
if guess in self.word:
for i in range(len(self.word)):
if guess == self.word[i]:
self.empty[i] = guess
print(guess)
class HangmanApp(App):
def build(self):
game = HangmanBoard()
Clock.schedule_interval(game.update, 1)
return game
if __name__ == '__main__':
HangmanApp().run() here
我千伏文件:
<HangmanBoard>:
orientation: 'vertical'
Button:
id: labelA
font_size: 60
size_hint: 1, 0.5
TextInput:
id: labelB
font_size: 60
pos_hint: {'center_x':0.5}
size_hint: 1, 0.5
focus: True
multiline: False
非常感謝! – SolaOmi
沒問題!考慮接受答案。我還有一個「當用戶停止輸入」機制的項目,所以如果你需要的話,我可以拉起相關的部分。 –