0

This is probably the wrong way of going about this completely, so I am open to other suggestions. I am trying to change the background of a button in another class from a button press. The button .kv is here:

Button:
            root: 'landing_sc'
            id: filebutton
            size: 150, 150
            size_hint: None, None
            background_normal: 'folder.png'
            background_down: 'opacity.png'
            pos_hint: {'x': 0.11, 'top': .7}
            on_release: 
                root.manager.transition = FadeTransition()
                root.manager.transition.duration = 1.5
                root.IfFolder()
                root.ChangeToSlide()
                app.switch.change()

And here is the app class it is referring to:

class MySubApp(App):
    def switch(self):
        change = LandingScreen()

    def build(self):
        return MyScreenManager()

And lastly here are the methods that I am trying to use in the LandingScreen class to change the background of a button there:

class LandingScreen(Screen):
    def __init__(self, **kwargs):
        super(LandingScreen, self).__init__(**kwargs)
        self.buttons = [] # add references to all buttons here
        Clock.schedule_once(self._finish_init)

def callfun(self, *args):
    self.ChangePic()

    def ChangePic(self, *args):
            self.buttons[1].background_normal = 'folder.png'

This seems like a massive work around that isn't working anyways. Is there a simpler way to change the attribute of a button in another class on_release? Thanks.

Austin
  • 347
  • 7
  • 21

1 Answers1

0

There's a lot of ways you could achieve this. One way is to use a property as demonstrated below. Also, if you are wondering why you aren't getting the expected color, read This Q and A.

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.properties import BooleanProperty
from kivy.lang import Builder

Builder.load_string("""
<MyButton>:
    background_color: [1, 0, 0, 1] if self.active else [1, 1, 1, 1]

<RootWidget>:
    orientation: "vertical"
    GridLayout:
        cols: 2
        size_hint: (1, .2)
        MyButton:
            text: "Screen One"
            on_release: root.sm.current = "one"
            active: root.sm.current == "one"
        MyButton:
            text: "Screen Two"
            on_release: root.sm.current = "two"
            active: root.sm.current == "two"
""")


class MyButton(Button):
    active = BooleanProperty(None)


class Manager(ScreenManager):

    def __init__(self, *args, **kwargs):
        super(Manager, self).__init__(*args, **kwargs)

        screen_one = Screen(name="one")
        screen_one.add_widget(Label(text="Screen One"))

        screen_two = Screen(name="two")
        screen_two.add_widget(Label(text="Screen Two"))

        self.add_widget(screen_one)
        self.add_widget(screen_two)


class RootWidget(BoxLayout):

    def __init__(self, *args, **kwargs):
        self.sm = Manager()
        super(RootWidget, self).__init__(*args, **kwargs)
        self.add_widget(self.sm)


class TestApp(App):

    def build(App):
        return RootWidget()


if __name__ == '__main__':
    TestApp().run()
Mox
  • 411
  • 3
  • 13