'Kivy action on key press and backspace
What I want to achieve in my program is the autofocus on the next TextInput boxes on press of the letter, however, while user uses "backspace" on an empty TextInput, I want my code to go back to the previous TextInput, focus there and delete of the char there. I was reading about keyboard_on_key_down but I don't really know how to use it.
Also I wanted to ask, whether is there simplier approach to creating multile TextInputs with certain ID.
GuessWord.kv
#:import TextInput kivy.uix.textinput.TextInput
<GuessWord>
RelativeLayout:
size: root.width, root.height
Label:
pos_hint: {'center_x':0.5,'y':0.7}
size_hint: 0.2,0.2
text: "Guess the word!"
Button:
pos_hint: {'center_x':0.5,'y':0.67}
size_hint: 0.5,0.1
text: "Click here to start!"
on_press: root.RandomWord()
GridLayout:
id: word
cols:5
pos_hint:{'center_x':0.5,'y':0.5}
size_hint:(1,0.15)
TextInput:
halign: "center"
font_size: 70
id: t1
on_text: t2.focus = True
TextInput:
halign: "center"
font_size: 70
id: t2
on_text: t3.focus = True
TextInput:
halign: "center"
font_size: 70
id: t3
on_text: t4.focus = True
TextInput:
halign: "center"
font_size: 70
id: t4
on_text: t5.focus = True
TextInput:
halign: "center"
font_size: 70
id: t5
GridLayout:
cols:1
pos_hint:{'center_x':0.5,'y':0.3}
size_hint:(1,0.2)
Button:
pos_hint:{'center_x':0.5,'y':0.4}
text: "Check if it's your word!"
on_press:
root.Check()
root.ShowPreviousWord()
Label:
pos_hint:{'center_x':0.5,'y':-0.35}
id: OUTPUT
Label:
pos_hint:{'center_x':0.2,'y':-0.35}
id:ATTEMPTS
GuessWord.py
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.config import Config
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.app import App
from random import choice
Builder.load_file('GuessWord.kv')
class GuessWord(Widget):
words=["words","crown","ideal"]
word=choice(words)
attempts=0
listofguesses=[]
def RandomWord(self):
self.attempts=0
self.word=choice(self.words)
l1=self.ids['OUTPUT']
l1.text=""
t1=self.ids['t1']
t2=self.ids['t2']
t3=self.ids['t3']
t4=self.ids['t4']
t5=self.ids['t5']
listofletters=[t1,t2,t3,t4,t5]
for i in range(5):
listofletters[i].background_color=(1,1,1,1)
listofletters[i].text=""
def ShowPreviousWord(self):
t1=self.ids['t1']
t2=self.ids['t2']
t3=self.ids['t3']
t4=self.ids['t4']
t5=self.ids['t5']
listofletters=[t1,t2,t3,t4,t5]
string=""
for i in listofletters:
string+=i.text
self.listofguesses.append(string)
attempts=""
for i in range(len(self.listofguesses)):
attempts+=f'Attempt {i+1}: {self.listofguesses[i]}\n'
l1=self.ids['ATTEMPTS']
l1.text=attempts
def Check(self):
self.attempts+=1
t1=self.ids['t1']
t2=self.ids['t2']
t3=self.ids['t3']
t4=self.ids['t4']
t5=self.ids['t5']
listofletters=[t1,t2,t3,t4,t5]
outputstring=""
accurateletters=0
for i in range(5):
listofletters[i].background_color=(1,1,1,1)
for i in range(len(listofletters)):
if listofletters[i].text.lower()=="":
outputstring+=f'You have not filled {i+1} letter\n'
elif listofletters[i].text.lower() in self.word:
if listofletters[i].text.lower() == self.word[i]:
listofletters[i].background_color=(0, 255/256, 0, 1)
accurateletters+=1
outputstring+=f'{listofletters[i].text.upper()} - letter in accurate spot\n'
else:
listofletters[i].background_color=(228/256, 245/256, 39/256, 1)
outputstring+=f'{listofletters[i].text.upper()} - letter is in word, but not in this spot\n'
else:
outputstring+=f'{listofletters[i].text.upper()} - no letter in the word\n'
listofletters[i].bacground_color=(1,1,1,1)
l1=self.ids['OUTPUT']
if accurateletters==5:
l1.text=f'You have won! You guessed the word after {self.attempts} attempts'
else:
l1.text=outputstring
class GuessWordApp(App):
def build(self):
return GuessWord()
if __name__ == '__main__':
GuessWordApp().run()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
