首页 > 解决方案 > Kivy:函数\方法不能从另一个类工作

问题描述

我制作了两个具有相同功能路径的按钮。但只有一个按钮有效。两个按钮都应更改lines.circle

我在其他帖子上读过,我可能需要将这两个课程“链接”在一起,以便他们可以“交谈”。如果有人有任何见解,请告诉我!

另外,为什么该print功能在两个按钮上都有效,而在其余功能上无效?

派:

from kivy.app import App
from kivy.properties import ListProperty
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen

class Screen1(Screen):
    pass

class SM(ScreenManager):
    pass

class ButtonX(Button):
    def increaseX(self):
        TestApp().increase()

class TestApp(App):

    random_num_list = ListProperty([20, 40, 80])

    def increase(self):
        print(str(self.random_num_list))
        self.random_num_list = [90, 140, 220]

    def build(self):
        global sm
        sm = SM()
        return sm

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

千伏:

<Screen1>:
    FloatLayout:
        Button:
            pos_hint: {'center_x': .5, 'center_y': .5}

            canvas:
                Color:
                    rgba: 1,1,1,1
                Line:
                    width: 5
                    circle: self.center_x, self.center_y, min(self.width, self.height) / 4, 0, app.random_num_list[0]

                Color:
                    rgba: .8,.1,.8,1
                Line:
                    width: 5
                    circle: self.center_x, self.center_y, min(self.width, self.height) / 4, app.random_num_list[0], app.random_num_list[1]

                Color:
                    rgba: .6,.6,1,1
                Line:
                    width: 5
                    circle: self.center_x, self.center_y, min(self.width, self.height) / 4, app.random_num_list[1], app.random_num_list[2]

                Color:
                    rgba: 1,.1,.4,1
                Line:
                    width: 5
                    circle: self.center_x, self.center_y, min(self.width, self.height) / 4, app.random_num_list[2], 360

        ButtonX:
            pos_hint: {'center_x': .5, 'center_y': .55}
            size_hint: .2, .1
            text: 'Try first:\ndoesn\'t works'
            on_press: self.increaseX()

        ButtonX:
            pos_hint: {'center_x': .5, 'center_y': .45}
            size_hint: .2, .1
            text: 'Try second:\nworks'
            on_press: app.increase()
<SM>:
    Screen1:

标签: pythonkivy

解决方案


为了理解这个问题,我将修改代码:

# ...
class ButtonX(Button):
    def increaseX(self):
        t2 = TestApp()
        print("t2", t2)
        t2.increase()
# ...
if __name__ == '__main__':
    t1 = TestApp()
    print("t1", t1)
    t1.run()

获得以下内容:

...
t1 <__main__.TestApp object at 0x7f8e750c38d0>
...
t2 <__main__.TestApp object at 0x7f8e71246320>
...

很明显,它们是 2 个不同的对象,因此在调用increase()它时不会对当前应用程序产生任何修改。

在 .kv 的情况下,app它是一个指示正在运行的应用程序的对象,在 .py 中等效的是App.get_running_app(),因此解决方案是使用最后一个元素。

from kivy.app import App
from kivy.properties import ListProperty
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen

class Screen1(Screen):
    pass

class SM(ScreenManager):
    pass

class ButtonX(Button):
    def increaseX(self):
        App.get_running_app().increase() # <---

class TestApp(App):
    random_num_list = ListProperty([20, 40, 80])

    def increase(self):
        print(str(self.random_num_list))
        self.random_num_list = [90, 140, 220]

    def build(self):
        sm = SM()
        return sm

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

最后,不要使用全局变量,因为它们在调试时很头疼。


推荐阅读