Kivy: How To Display 'Correct!' When Input Is Correct?
I'm trying to create a system that will show 'correct' when the input is correct. But I'm so confused about how classes and functions work even after watching tutorials and reading
Solution 1:
Here is an example of how you can use a TextInput
widget to get some user input. Then you need to define a function that will check what the user put in to the name (the source
) of your Image
widget. Call that function with some button to check the user input. Here is a very short example of how to do that (make sure the files are named main.py and main.kv)
main.py
from kivy.app import App
class MainApp(App):
def check_answer(self, text_to_check, *args):
# You can ignore what is held in *args, but keep it there
# Get the name of the image
the_image = self.root.ids['the_image']
the_image_name = the_image.source
# Get the user's input
print("the image name was: ", the_image_name)
print("Your guess was: ", text_to_check)
if the_image_name == text_to_check:
print("Correct!")
else:
print("Incorrect :(")
MainApp().run()
main.kv
GridLayout:
cols: 1
Image:
id: the_image
source: "a.png"
TextInput:
id: the_text_input
hint_text: "Type your answer here"
Button:
text: "Check Answer"
on_release:
# Call the function we defined in the python file
# Pass the text that the user put in by referencing the id of the
# TextInput and getting the value of the text
app.check_answer(the_text_input.text)
Post a Comment for "Kivy: How To Display 'Correct!' When Input Is Correct?"