KIVY 在外部条件下循环遍历图像

KIVY loop through images on external condition

我正在尝试使用 KIVY 和 gTTS 制作一个 children 的学习应用程序,其中 child 将显示随机图像并且必须通过说出它是什么来识别它(“方形" 表示正方形,"三" 表示 3 等)。

到目前为止,我的菜单工作正常。 我在字典中使用 random.choice(),其中值是图像路径,键是“名称”

如果我打开相关屏幕,图像会随机正确选择并使用 def on_pre_enter(self, *args): 显示,gTTS 也可以正常使用 def on_enter(self, *args): 但只有一次

我希望它在用户回复前一个图像循环 X 次后加载一个新的随机图像,但无论我尝试什么我都无法让它工作(我想把所有东西都放在 for x in range() 循环以及在 while X < Y: 上使用计数器但没有任何成功)。

这是我的 .py 文件

import random
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty


class MenuWindow(Screen):
    pass

class ShapeGame(Screen):
    rand_shape = StringProperty()
    def on_pre_enter(self, *args):        
        random_shape = {"square":'shapes/square.png', "triangle":'shapes/triangle.jpg', "circle":'shapes/circle.jpg'}
        random_shape_key, random_shape_value = random.choice(list(random_shape.items()))
        print(random_shape_key)
        self.rand_shape_key = random_shape_key
        self.rand_shape = random_shape_value
        
    def on_enter(self, *args):
        print(self.random_shape_key)

class WindowManager(ScreenManager):
    pass

class MainApp(App):  
    def build(self):
        return Builder.load_file('Main.kv')

if __name__ == '__main__':
    MainApp().run()

和我的 .kv 文件

#:kivy 2.0
WindowManager:
    MenuWindow:
    ShapeGame:

<MenuWindow>:
    name: "menu"
    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height

        Label:
            id:"menu"
            text: "Menu Screen"
            font_size: 34
            
        BoxLayout:
            size_hint: 1.0, 0.2
            Button:
                text: "Shape Game"
                font_size: 22
                on_release:
                    app.root.current = "shapes"
                    root.manager.transition.direction = "left"
                
        Button:
            text: "Exit"
            font_size: 22
            size_hint: 1.0, 0.2
            on_release: app.root.current = exit() 

<ShapeGame>:
    name: "shapes"
    id: ShapeGame
    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height
        Image:
            id:"shapes"
            screen: ShapeGame
            source: self.screen.rand_shape
            before_source: self.source

        BoxLayout:
            size_hint: 1.0, 0.2
            size: root.width, root.height
            Button:
                text: "Menu"
                on_release:
                    app.root.current = "menu"
                    root.manager.transition.direction = "right"
            Button:
                text: "Exit"
                on_release:
                    app.root.current = exit()

the entire repo

不确定我是否可以解决你的问题,假设你想在一段时间后在进入屏幕时随机播放图像,直到某个值达到其限制。

为此你可以使用 Clock.schedule_interval 一些等待时间,比如 2.0 秒。

因此您的 ShapeGame 现在看起来像,

class ShapeGame(Screen):
    rand_shape = StringProperty()
    count = 0

    def on_pre_enter(self, *args):
        self.count = 0
        self.change_event = Clock.schedule_interval(self.chage_photo, 2.0)
        
    def chage_photo(self, *args):
        if self.count < 3:
            self.count += 1
        else:
            self.change_event.cancel()
        random_shape = {"square":'shapes/square.png', "triangle":'shapes/triangle.jpg', "circle":'shapes/circle.jpg'}
        random_shape_key, random_shape_value = random.choice(list(random_shape.items()))
        print(random_shape_key)
        self.rand_shape_key = random_shape_key
        self.rand_shape = random_shape_value

您应该将 source: self.screen.rand_shape 更改为 source: root.rand_shape

您也可以通过按钮触发相同的操作,而不是使用 Clock.schedule