如何在回调时替换Kivy小部件? [英] How To Replace Kivy Widgets On Callback?

查看:63
本文介绍了如何在回调时替换Kivy小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to Python and Kivy, and I'm trying to create multipage display of letters of the braille alphabet, with the corresponding braille's letter picture present in every page. I really want to learn more about creating Kivy desktop apps. I really hope you can help me. What I'm trying to do is have a page look like this:

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

I know how images and buttons are placed and customized in terms of size and position in the KV file. However what I need to learn is how add_widget() and clear_widget() will factor in this. I have read the Kivy docs but they barely explain how I could achieve what I need. What I thought of doing is using the from kivy.uix.screenmanager import ScreenManager, Screen feature, and then just create 26 screens and route them via on_click in the kv file. But that's tedious and too manual. Here's my code so far:

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中的工作方式.我希望你能帮助我.谢谢.

I haven't gotten around the kv file because i'm clueless how this will pan out. What I need to do is create widgets or a function that will swap out the images of the current letter and display those of the next or previous ones when the next/button is clicked, without having to switch screens every single time. I'm really unfamiliar with how functions work in Kivy and Python. I hope you could help me. Thank you.

推荐答案

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

Here is a simple solution to your problem. I'll leave it to you to modify and make it look and work exactly how you want :)

学习kv语言非常有帮助,容易,并且可以很快掌握.

Learning the kv language is INCREDIBLY helpful, easy, and it can be picked up quite quickly.

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

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

I'm happy to address specific questions if you have them.

这篇关于如何在回调时替换Kivy小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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