在Kivy中引用另一个类的函数 [英] Referencing a Function From Another Class in Kivy

查看:216
本文介绍了在Kivy中引用另一个类的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我的代码和它的功能:

 标签:
id:easyscore
text:0
font_size:44
bold:True
color:[1,1,1 1]

这个标签位于一个名为easy的屏幕类中,它计算得分。

  Button:
text:Restart
font_size:32
bold:True
color:[1,1,1]
background_normal:
background_color:[0.31,0.4,0.63,1]
on_release:root.rst_gmvr()

这个按钮位于一个名为gameovereasy的屏幕类中,当它被释放时,它应该将屏幕变回容易状态并将分数计数器重置为零。



以前的两段代码是用.kv文件写的,以下两段用.py文件写入



它调用的函数叫做rst_gmvr,它是在gameovereasy屏幕类中,它看起来像:

  def rst_gmvr(self):
easy()。rec_rst()

函数然后调用一个名为rec_rst的函数,它位于easy screen类中,它看起来像:

  def rec_rst(self):
self.ids.easyscore.text =0
sm.current =easy



因此,这个函数应该将称为easyscore的分数计数器标签重置为0,并将屏幕更改为简单。然而,它只会改变屏幕而不是分数计数器的标签。



有人可以帮助我了解如何在释放按钮时更改屏幕和分数计数器标签。

感谢:)

顺便说一句,当按钮被释放时,不会给出错误信息。如果您需要更多的信息和/或代码来回答这个问题,我会很乐意给您。

你说,你在两个不同的类中有两个函数,所以你需要让这些类互相看到。基本上你或者需要在另一个函数中访问一个函数,或者在你的情况下,你需要另一个类来维护你所有的类。



第二个选项已经在那里了,你只需要键入一些东西,因为类是用于每个应用程序的 App()。将类 easy gameovereasy 分配给 App()并使用通过应用程序调用通过 get_running_app () 像这样:

  class My(...):
def __init __(self,** kwargs):
self.app = App.get_running_app()
self.my = self.app.my
$ b $ class MyAppClass(App ):
my = My()
def build(self):
...

或直接使用 self.my = App.get_running_app()。my ,然后使用 my.function() 。您需要将它添加到您想与之沟通的每个课程中。


So this is my code and what it does:

Label:
    id: easyscore
    text: "0"
    font_size: 44
    bold: True
    color: [1, 1, 1, 1]

This label is on a screen class called easy and it counts the score.

Button:
    text: "Restart"
    font_size: 32
    bold: True
    color: [1, 1, 1, 1]
    background_normal: ""
    background_color: [0.31, 0.4, 0.63, 1]
    on_release: root.rst_gmvr()

This button is on a screen class called gameovereasy and when it is released it should change the screen back to easy and reset the score counter back to zero.

The previous 2 blocks of code were written in a .kv file, the following 2 are written in a .py file

The function it calls is called rst_gmvr and it's is in the gameovereasy screen class and it looks like:

def rst_gmvr(self):
    easy().rec_rst()

This function then calls a function called rec_rst which is in the easy screen class and it looks like:

def rec_rst(self):
    self.ids.easyscore.text = "0"
    sm.current = "easy"

So therefore this function should reset the score counter label called easyscore back to 0 and change the screen to easy. However, it only changes the screen and not the score counter label.

Can someone help me understand how to change both the screen and the score counter label when the button is released.

Thanks :)

By the way, when the button is released no error messages are given. If you need any more information and/or code to answer this I'll be happy to give it to you.

解决方案

As you say, you have two functions in two different classes, therefore you need to make the classes see each other. Basically you either need to access one function in another when it's something small, or in your case you'd need another class that would maintain all your classes.

The second option is already there, you just need to type something, because the class is App() which you use for every application. Assign classes easy and gameovereasy to App() and use the call through application via get_running_app() like this:

class My(...):
    def __init__(self, **kwargs):
        self.app = App.get_running_app()
        self.my = self.app.my

class MyAppClass(App):
    my = My()
    def build(self):
        ...

or directly self.my = App.get_running_app().my and then call the function with my.function(). You need add it to every class you want to communicate with.

这篇关于在Kivy中引用另一个类的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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