Kivy:如何显示“正确!”输入正确时?

时间:2019-04-09 03:28:58

标签: android python kivy

我正在尝试创建一个在输入正确时将显示“正确”的系统。但是,即使在观看了教程并阅读了文档之后,我还是对类和函数的工作方式感到困惑,因为我是Python和Kivy的新手。

到目前为止,这是我的kv代码

<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'

和我的.py文件(不包括不必要的部分)

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()

我只是首先格式化了所有内容,以便我知道它们的放置位置,但是我不知道从这里取出它。我的.py文件只是对我包含在kv文件中的所有类使用了“传递”。我不知道如何实现类和函数来实现我所需要的。

输入将来自键盘输入,并将根据字典确定字母是否与图片匹配。如果字母与图片匹配,则图片应与CorrectLayout一起出现在图片旁边。然后,当用户单击LetterAscreen中的下一个并前进至LetterBScreen,然后单击上一个时,我需要LetterAScreen才能恢复为没有CorrectLayout和第二个图像

有人会帮助我吗?好吗?

1 个答案:

答案 0 :(得分:0)

这里是一个示例,说明如何使用TextInput小部件来获取用户输入。然后,您需要定义一个函数,该函数将检查用户在source小部件名称(Image)中输入了什么。使用某个按钮调用该功能以检查用户输入。这是一个非常简短的示例(确保文件名为 main.py 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)