我一直在玩kivy图书馆并且一直在尝试为游戏刽子手[1]创建一个GUI。我尝试创建一个用户输入单个字母的TextField,当按下return / enter时,它会更改按钮文本 ONLY ,而不是textinput.text提供的常量流。< / p>
我查看了文档和其他stackoverflow Q&amp; A&s,似乎解决方案在于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
答案 0 :(得分:0)
是的,当用户停止输入1秒钟时,您可以拥有&#34;&#34;有时钟事件的机制或只需要on_text_validate
更新按钮的文本,后者更简单,更直接:
TextInput:
on_text_validate:
labelA.text = self.text
# ...