0
私はkivyライブラリで遊んでいて、ゲームのハングマン[1]のGUIを作成しようとしています。私はTextFieldを作成しようとしているのですが、ユーザが1文字を入力してボタンのテキストを変更するのは、テキスト入力のテキストの代わりにreturn/enterを押すとのみです。(Kivy/Python)ユーザーが入力したときにtextinputウィジェットからテキストを保存
私はドキュメントや他のstackoverflow 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
私のKvファイル:
<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
問題ありません!答えを受け入れることを検討してください。また、「ユーザーが入力をやめるのをやめた」というメカニックもいるので、必要な場合は関連する部品を引き上げることができます。 –