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
classMainApp(App):
defcheck_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 inputprint("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:1Image:id:the_imagesource:"a.png"TextInput:id:the_text_inputhint_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 textapp.check_answer(the_text_input.text)
Post a Comment for "Kivy: How To Display 'correct!' When Input Is Correct?"