Kivy:如何显示“正确!"什么时候输入正确? [英] Kivy: How To Display 'Correct!' When Input Is Correct?

查看:87
本文介绍了Kivy:如何显示“正确!"什么时候输入正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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'

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

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

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

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.

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

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?

推荐答案

下面是一个示例,说明如何使用TextInput小部件获取用户输入.然后,您需要定义一个函数,该函数将检查用户在Image小部件名称(source)中输入了什么.使用某个按钮调用该功能以检查用户输入.这是一个简短的示例(确保文件名为 main.py main.kv )

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)

这篇关于Kivy:如何显示“正确!"什么时候输入正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆