【问题标题】:Kivy: How To Display 'Correct!' When Input Is Correct?Kivy:如何显示“正确!”什么时候输入正确?
【发布时间】:2019-04-09 03:28:58
【问题描述】:

我正在尝试创建一个在输入正确时显示“正确”的系统。但是我对类和函数的工作方式感到非常困惑,即使在观看教程和阅读文档之后,因为我是 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 和第二张图片。

有人会帮助我吗?请问?

【问题讨论】:

  • 请帮帮我,我真的需要帮助

标签: android python kivy


【解决方案1】:

下面是一个示例,说明如何使用 TextInput 小部件获取一些用户输入。然后您需要定义一个函数来检查用户在您的Image 小部件的名称(source)中输入的内容。使用某个按钮调用该函数以检查用户输入。这是一个非常简短的示例,说明如何执行此操作(确保文件名为 main.pymain.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)

【讨论】:

  • 再次感谢您再次帮助我先生!很大的帮助,太多了。我有一个小后续问题。如果我有 26 个字母的 26 个不同的 Image png,我是否必须使用不同的 id 来调用它们?
  • 如果我是你,当你想显示另一个图像时,我会更改 Image 小部件的 source: 属性(显示的文件)。这样id 没有改变,它仍然是the_image,但是当您检查the_image.source 时,它将是新图像的名称
  • 所以你的“NEXT”按钮将不再改变屏幕,它只会设置the_image.source = "b.png"。这是最简单的事情。更通用的方法是将source 设置为非硬编码的内容。也许你会将它设置为“random_alphabet_letter.png”,或者像我在你之前的问题中写的next_letter 函数一样获取字母表中的下一个字母
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多