AttributeError Button object has no attribute scrlFBtn

2024/10/14 1:15:37
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 Buttonclass BSGameMain:def sas(self):# blmain.remove_widget(scrlFBtns)self.scrlFBtns.remove_widget(blbtns)blmain = BoxLayout(orientation = 'vertical') # MainBoxLayout initscrlFBtns = ScrollView(effect_cls = 'ScrollEffect')blbtns = BoxLayout(orientation = 'vertical',size_hint_y = None) # BoxLayout for buttonsblbtns.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.blmainif __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 =)

Answer

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 Buttonclass BSGameMain:def __init__(self):self.blmain = BoxLayout(orientation = 'vertical') # MainBoxLayout initself.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.blmainif __name__ == "__main__":BSApp().run()
https://en.xdnf.cn/q/118014.html

Related Q&A

How to set interpreter of WinPython as the vim default python interpreter?

I use a Python distribution named WinPython. Now I want my vim to use the python interpreter in WinPython as its default interpreter. I tried add the F:\WinPython\python-2.7.3.amd64 into my windows env…

how to deal with Python BaseHTTPServer killed,but the port is still be occupied?

I use python BaseHTTPServer,it can handle do_GET,do_POST methods,in do_POST method,i execute linux shell using os.system,when i kill the python script,but the listening port still occupied,so i cant ru…

Circular imports and class fields in python3

Okay, I do understand that this topic is old as hell, but I couldnt find an answer to the particular question that I am asking.Lets say that we have a very simple structure: two files, a.py and b.py, t…

Bad HTTP response returned from the server. Code 500

I have a problem to use pywinrm on linux, to get a PowerShell Session. I read several posts and questions on sites about that. But any that can solve my question. The error is in the Kerberos autenti…

Iterate one list of synsets over another

I have two sets of wordnet synsets (contained in two separate list objects, s1 and s2), from which I want to find the maximum path similarity score for each synset in s1 onto s2 with the length of outp…

Flask werkzeug.routing.BuildError

I doing a flask app and when i try to put a link to redirect a user to his profile page by callingBuildError: Could not build url for endpoint profile. Did you forgetto specify values [business_name]?…

getting attribute of an element with its corresponding Id

suppose that i have this xml file :<article-set xmlns:ns0="http://casfwcewf.xsd" format-version="5"> <article><article id="11234"><source><hostn…

How to install selenium python on Mac

Ive downloaded the Selenium zip file for python and it contains the folder with the setup.py. It says on python.org that I have to type in terminal python setup.py install but it gives me this error th…

aws s3 - object has no attribute server_side_encryption

Can someone please explain the differences in these two calls. The first one gives the correct server_side_encryption and the second one gives an error. The other attributes give the same value-#!/usr/…

Removing nested for loop to find coincidence values

I am currently using a nested for loop to iterate through to arrays to find values that match a certain criterion. The problem is that this method is incredibly inefficient and time consuming. I was to…