Search code examples
pythonbuttonkivyattributeerror

AttributeError 'Button' object has no attribute scrlFBtn


from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.scrollview import ScrollView
from kivy.effects.scroll import ScrollEffect
from kivy.uix.widget import Widget
from kivy.uix.button import Button

class BSGameMain:
    def sas(self):
        # blmain.remove_widget(scrlFBtns)
        self.scrlFBtns.remove_widget(blbtns)

    blmain = BoxLayout(orientation = 'vertical') # MainBoxLayout init

    scrlFBtns = ScrollView(effect_cls = 'ScrollEffect')

    blbtns = BoxLayout(
        orientation = 'vertical',
        size_hint_y = None
        ) # BoxLayout for buttons

    blbtns.bind(minimum_height = blbtns.setter('height'))
    scrlFBtns.add_widget(blbtns)

    for i in range (2):
        blbtns.add_widget(Button(
            text='asd',
            size_hint_y = None,
            height = 40,
            on_press = sas
            ))

    lblmain = Label(text = 'asd')
    blmain.add_widget(lblmain)
    blmain.add_widget(scrlFBtns)


class BSApp(App):
    def build(self):
        game = BSGameMain()
        return game.blmain

if __name__ == "__main__":
    BSApp().run()

AttributeError 'Button' object has no attribute scrlFBtn. What is the problem? I'm trying to make it so that when you clicked, the screen was cleared (Widget was deleted). Рelp me please =)


Solution

  • Your code has several errors and bad programming practices:

    • if you declare variables that are inside a class and outside any method of the class will be class variables and not attributes of the class, so it is not a good practice to do so if you want to use later self, all that code must be within a method of a class.

    • on_someproperty wait as parameter a function that receives parameters, in your case sas() does not receive them so the solution is to use a lambda method.


    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.label import Label
    from kivy.uix.scrollview import ScrollView
    from kivy.effects.scroll import ScrollEffect
    from kivy.uix.button import Button
    
    class BSGameMain:
        def __init__(self):
            self.blmain = BoxLayout(orientation = 'vertical') # MainBoxLayout init
    
            self.scrlFBtns = ScrollView(effect_cls = 'ScrollEffect')
    
            self.blbtns = BoxLayout(
                orientation = 'vertical',
                size_hint_y = None )
    
            self.blbtns.bind(minimum_height = self.blbtns.setter('height'))
            self.scrlFBtns.add_widget(self.blbtns)
    
            for i in range(2):
                self.blbtns.add_widget(Button(
                    text='asd',
                    size_hint_y = None,
                    height = 40,
                    on_press = lambda *args: self.sas()))
    
            lblmain = Label(text = 'asd')
            self.blmain.add_widget(lblmain)
            self.blmain.add_widget(self.scrlFBtns)
    
        def sas(self):
            self.scrlFBtns.remove_widget(self.blbtns)
    
    
    class BSApp(App):
        def build(self):
            game = BSGameMain()
            return game.blmain
    
    if __name__ == "__main__":
        BSApp().run()