如何在回调时替换Kivy小部件?

时间:2019-04-05 14:33:09

标签: python oop widget kivy

我是Python和Kivy的新手,我试图创建盲文字母的多页显示,并在每页中显示相应的盲文字母图片。我真的想了解有关创建Kivy桌面应用程序的更多信息。我真的希望你能帮助我。我想做的是让页面看起来像这样:As

我知道如何根据KV文件中的大小和位置放置和自定义图像和按钮。但是,我需要学习的是add_widget()clear_widget()将如何考虑这一点。我已经阅读过Kivy文档,但是他们几乎没有解释我如何实现我所需要的。我想做的是使用from kivy.uix.screenmanager import ScreenManager, Screen功能,然后仅创建26个屏幕并通过kv文件中的on_click路由它们。但这很乏味而且太手工了。到目前为止,这是我的代码:

class LetterAScreen(Screen):
    pass

class LetterBScreen(Screen):
    pass

class LetterCScreen(Screen):
    pass

class LetterDScreen(Screen):
    pass

class LetterEScreen(Screen):
    pass

class LetterFScreen(Screen):
    pass

class LetterGScreen(Screen):
    pass

 #.... so and so until Letter Z




sm = ScreenManager(transition=SwapTransition())

#LearnScreen - Alphabet
sm.add_widget(LetterAScreen(name='lettera'))
sm.add_widget(LetterBScreen(name='letterb'))
sm.add_widget(LetterCScreen(name='letterc'))
sm.add_widget(LetterDScreen(name='letterd'))
sm.add_widget(LetterEScreen(name='lettere'))

sm.add_widget(LetterFScreen(name='letterf'))
sm.add_widget(LetterGScreen(name='letterg'))
sm.add_widget(LetterHScreen(name='letterh'))
sm.add_widget(LetterIScreen(name='letteri'))
sm.add_widget(LetterJScreen(name='letterj'))
sm.add_widget(LetterKScreen(name='letterk'))
sm.add_widget(LetterLScreen(name='letterl'))

sm.add_widget(LetterMScreen(name='letterm'))
sm.add_widget(LetterNScreen(name='lettern'))
sm.add_widget(LetterOScreen(name='lettero'))
sm.add_widget(LetterPScreen(name='letterp'))
sm.add_widget(LetterQScreen(name='letterq'))
sm.add_widget(LetterRScreen(name='letterr'))

sm.add_widget(LetterSScreen(name='letters'))
sm.add_widget(LetterTScreen(name='lettert'))
sm.add_widget(LetterUScreen(name='letteru'))
sm.add_widget(LetterVScreen(name='letterv'))
sm.add_widget(LetterWScreen(name='letterw'))
sm.add_widget(LetterXScreen(name='letterx'))
sm.add_widget(LetterYScreen(name='lettery'))
sm.add_widget(LetterZScreen(name='letterz'))

我还没听懂kv文件,因为我不知道该怎么办。我需要做的是创建小部件或函数,该小部件或函数将交换当前字母的图像,并在单击下一个/按钮时显示下一个或上一个字母的图像,而不必每次都切换屏幕。我真的不熟悉函数在Kivy和Python中的工作方式。我希望你能帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

这里是解决您的问题的简单方法。我将它留给您进行修改,使其外观和工作方式完全符合您的要求:)

学习kv语言非常有用,容易,并且可以很快掌握。

main.py

from kivy.app import App

class MainApp(App):
    alphabet = 'abcdefghijklmnopqrstuvwxyz'

    def next_letter(self):
        # Get a reference to the widget that shows the letters
        # self.root refers to the root widget of the kv file -- in this case,
        # the GridLayout
        current_letter_widget = self.root.ids['the_letter_label']
        # Get the letter currently shown
        current_letter = current_letter_widget.text
        # Find the next letter in the alphabet
        next_letter_index = self.alphabet.find(current_letter) + 1
        next_letter = self.alphabet[next_letter_index]

        # Set the new letter in the widget that shows the letters
        current_letter_widget.text = next_letter

MainApp().run()

main.kv

GridLayout: # This is the `root` widget of the main app class
    cols: 1
    Label:
        text: "g"
        id: the_letter_label # Setting an id for a widget lets you refer to it later
    Button:
        text: "Previous"
    Button:
        text: "Next"
        on_release:
            # the keyword `app` references the main app class, so we can call
            # the `next_letter` function
            app.next_letter()

如果您有特定的问题,我很乐意解决。