问题
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 through documentations, as I'm new to Python and Kivy.
This is my kv code so far
<CorrectLayout>
id: correctlayout
Label:
text: 'Gesture Correct!'
background_normal:'bgpics/translateback.jpg'
pos_hint:{"x":-0.15,"y":-.43}
color: 1,1,0,1,1
font_size: '45sp'
font_name: 'EraserRegular.ttf'
Image:
source: 'bgpics/check2.png'
pos_hint:{"x":0.64,"y":.03}
size_hint: .1, .1
allow_stretch: True
keep_ratio: False
<LetterAScreen>:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'bgpics/bluebg.jpg'
CorrectLayout:
FloatLayout:
Label:
text: 'Learning the FSL Alphabet'
background_normal:'bgpics/chalk2.png'
pos_hint:{"x":0.009,"y":.43}
font_size: '45sp'
font_name: 'SqueakyChalkSound.ttf'
Image:
source: 'handgesture/a.png'
pos_hint:{"x":0.009,"y":.15}
size_hint: .40, .70
allow_stretch: True
keep_ratio: False
Image:
source: 'handgesture/a.png'
pos_hint:{"x":0.43,"y":.15}
size_hint: .40, .70
allow_stretch: True
keep_ratio: False
Button:
text: "NEXT"
background_normal:'bgpics/translateback.jpg'
font_size: '35sp'
font_name: 'vtks.ttf'
color: 0, 0, 0, 1
pos_hint:{"x":.87,"y":.6}
size_hint: .1, .1
on_press: root.manager.current = 'letterb'
Button:
text: "QUIT"
background_normal:'bgpics/translateback.jpg'
font_size: '35sp'
font_name: 'vtks.ttf'
color: 0, 0, 0, 1
pos_hint:{"x":.87,"y":.2}
size_hint: .1, .1
on_press: root.manager.current = 'menu'
<LetterBScreen>:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'bgpics/bluebg.jpg'
CorrectLayout:
FloatLayout:
Label:
text: 'Learning the FSL Alphabet'
background_normal:'bgpics/chalk2.png'
pos_hint:{"x":0.009,"y":.43}
font_size: '45sp'
font_name: 'SqueakyChalkSound.ttf'
Image:
source: 'handgesture/b.png'
pos_hint:{"x":0.009,"y":.15}
size_hint: .40, .70
allow_stretch: True
keep_ratio: False
Image:
source: 'handgesture/b.png'
pos_hint:{"x":0.43,"y":.15}
size_hint: .40, .70
allow_stretch: True
keep_ratio: False
Button:
text: "NEXT"
background_normal:'bgpics/translateback.jpg'
font_size: '35sp'
font_name: 'vtks.ttf'
color: 0, 0, 0, 1
pos_hint:{"x":.87,"y":.6}
size_hint: .1, .1
on_press: root.manager.current = 'lettera'
Button:
text: "BACK"
background_normal:'bgpics/translateback.jpg'
font_size: '35sp'
font_name: 'vtks.ttf'
color: 0, 0, 0, 1
pos_hint:{"x":.87,"y":.4}
size_hint: .1, .1
on_press: root.manager.current = 'lettera'
Button:
text: "QUIT"
background_normal:'bgpics/translateback.jpg'
font_size: '35sp'
font_name: 'vtks.ttf'
color: 0, 0, 0, 1
pos_hint:{"x":.87,"y":.2}
size_hint: .1, .1
on_press: root.manager.current = 'menu'
and my .py file (did not include the unnecessary parts)
class CorrectLayout(FloatLayout):
pass
class LetterAScreen(Screen):
pass
class LetterBScreen(Screen):
pass
sm = ScreenManager(transition=SwapTransition())
sm.add_widget(LetterAScreen(name='lettera'))
sm.add_widget(LetterBScreen(name='letterb'))
class MainApp(App):
def build(self):
return sm
if __name__ == '__main__':
MainApp().run()
I just formatted everything in first so that I know where they would be placed, but I don't know where to take it from here. My .py file just used 'pass' to all the classes I included in the kv file. I don't know how to implement classes and functions to achieve what I need.
The input will come from a keyboard input, and will decide from a dictionary if the letter matches with the picture. If the letter is a match with the picture, then the picture should appear beside it, along with the CorrectLayout
. Then when the user clicks next in the LetterAscreen
and proceeds to the LetterBScreen
, then clicks previous, I need the LetterAScreen
to revert to not having the CorrectLayout
and the second image.
Will anyone help me? Please?
回答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)
来源:https://stackoverflow.com/questions/55584699/kivy-how-to-display-correct-when-input-is-correct