Object has no attribute in kivy gridlayout

Viewed 23

I am new to kivy and I have been stuck on this problem for about a week and I just can't seem to find a way around it

I have created a function in python that is supposed to get label text in kivy and print out the contents in the terminal when a button is released. However, whenever the function is called, I get AttributeError'ProfileWindow' object has no attribute 'getlabeltext'. All I want is to get the widget information in kivy like textinput, color, and text that I can later use to write my logic code with python. please help!

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import csv
from kivy.uix.scrollview import ScrollView
from kivy.uix.carousel import Carousel
from kivy.effects.kinetic import KineticEffect
from kivy.base import runTouchApp


class ProfileWindow(Screen):
    pass


class WindowManager(ScreenManager):
    pass


Build_LearnAB = Builder.load_file('samp.kv')


class Samp(App):
    def build(self):
        return Build_LearnAB
    def getlabeltext(self):
        print(self.button1.text)


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

below is my kivy code

#kivy 2.0.0
#:import ScrollView kivy.uix.scrollview

WindowManager:
    ProfileWindow:



################## PROFILE PAGE ############################

<ProfileWindow>:
    name: 'Profile'

    canvas.before:
        Color: 
            rgba: .94,1,.9, 1
        Rectangle:
            pos: self.pos
            size: self.size

    ScrollView:
        size_hint_y: .73
        pos_hint: {'x':0, 'y': .11}
        do_scroll_x: False
        do_scroll_y: True

        button1:button1
        GridLayout:
            size:(root.width, root.height)
            size_hint_x: None
            size_hint_y: None
            cols: 1
            height: self.minimum_height
            row_default_height: 180
            row_force_default: True

            Carousel:
                direction: 'right'

                AsyncImage:
                    source: '/Users/foxcoding/Desktop/TestApp/Images/cool.jpg'

                AsyncImage:
                    source: '/Users/foxcoding/Desktop/TestApp/Images/awesome.jpg'


            Button:
                id: button1
                text: 'Hello World'
                background_color: (1, 0, 0, 1)
                on_release: root.getlabeltext()

            Button:
                text: 'Hello World'
                background_color: (0, 0, 1, 1)

            Button:
                text: 'Hello World'
                background_color: (1, 0, 0, 1)

            Button:
                text: 'Hello World'
                background_color: (0, 0, 1, 1)

            Button:
                text: 'Hello World'
                background_color: (1, 0, 0, 1)

            Button:
                text: 'Hello World'
                background_color: (0, 0, 1, 1)
            Button:
                text: 'Hello World'
                background_color: (1, 0, 0, 1)

            Button:
                text: 'Hello World'
                background_color: (0, 0, 1, 1)
            Button:
                text: 'Hello World'
                background_color: (1, 0, 0, 1)

            Button:
                text: 'Hello World'
                background_color: (0, 0, 1, 1)

below is the error I experienced

C:\Users\Admin\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Admin/PycharmProjects/pythonProject/sample.py 
[INFO   ] [Logger      ] Record log in C:\Users\Admin\.kivy\logs\kivy_22-09-19_16.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.3.2
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.3.1
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.4.5
[INFO   ] [Kivy        ] v2.1.0
[INFO   ] [Kivy        ] Installed at "C:\Users\Admin\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.10.7 (tags/v3.10.7:6cc6b13, Sep  5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "C:\Users\Admin\PycharmProjects\pythonProject\venv\Scripts\python.exe"
[INFO   ] [Logger      ] Purge log fired. Processing...
[INFO   ] [Logger      ] Purge finished!
[INFO   ] [Factory     ] 189 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.6.0 - Build 30.0.101.1340'>
[INFO   ] [GL          ] OpenGL vendor <b'Intel'>
[INFO   ] [GL          ] OpenGL renderer <b'Intel(R) HD Graphics 520'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 6
[INFO   ] [GL          ] Shading version <b'4.60 - Build 30.0.101.1340'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [Loader      ] using a thread pool of 2 workers
[ERROR  ] [AsyncImage  ] Not found </Users/foxcoding/Desktop/TestApp/Images/awesome.jpg>
[ERROR  ] [AsyncImage  ] Not found </Users/foxcoding/Desktop/TestApp/Images/cool.jpg>
[WARNING] [Lang        ] The file C:\Users\Admin\PycharmProjects\pythonProject\samp.kv is loaded multiples times, you might have unwanted behaviors.
[ERROR  ] [AsyncImage  ] Not found </Users/foxcoding/Desktop/TestApp/Images/awesome.jpg>
[ERROR  ] [AsyncImage  ] Not found </Users/foxcoding/Desktop/TestApp/Images/cool.jpg>
[ERROR  ] [AsyncImage  ] Not found </Users/foxcoding/Desktop/TestApp/Images/awesome.jpg>
[ERROR  ] [AsyncImage  ] Not found </Users/foxcoding/Desktop/TestApp/Images/cool.jpg>
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
[INFO   ] [Base        ] Leaving application in progress...
 Traceback (most recent call last):
   File "C:\Users\Admin\PycharmProjects\pythonProject\sample.py", line 35, in <module>
     Samp().run()
   File "C:\Users\Admin\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\app.py", line 955, in run
     runTouchApp()
   File "C:\Users\Admin\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\base.py", line 574, in runTouchApp
     EventLoop.mainloop()
   File "C:\Users\Admin\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\base.py", line 339, in mainloop
     self.idle()
   File "C:\Users\Admin\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\base.py", line 383, in idle
     self.dispatch_input()
   File "C:\Users\Admin\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\base.py", line 334, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Users\Admin\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\base.py", line 302, in post_dispatch_input
     wid.dispatch('on_touch_up', me)
   File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\Admin\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\uix\behaviors\button.py", line 179, in on_touch_up
     self.dispatch('on_release')
   File "kivy\_event.pyx", line 727, in kivy._event.EventDispatcher.dispatch
   File "kivy\_event.pyx", line 1307, in kivy._event.EventObservers.dispatch
   File "kivy\_event.pyx", line 1191, in kivy._event.EventObservers._dispatch
   File "C:\Users\Admin\PycharmProjects\pythonProject\venv\lib\site-packages\kivy\lang\builder.py", line 55, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "C:\Users\Admin\PycharmProjects\pythonProject\samp.kv", line 51, in <module>
     on_release: root.getlabeltext()
   File "kivy\weakproxy.pyx", line 32, in kivy.weakproxy.WeakProxy.__getattr__
 AttributeError: 'ProfileWindow' object has no attribute 'getlabeltext'

Process finished with exit code 1
1 Answers

The problem is that your kv:

on_release: root.getlabeltext()

is trying to call getlabeltext() on the root object of the containing kv rule. The root of that rule is the ProfileWindow, but the ProfileWindow has no attribute getlabeltext (as the error notes). The fix is to give ProfileWindow a getlabeltext, like this:

class ProfileWindow(Screen):
    def getlabeltext(self):
        print(self.ids.button1.text)  # note use of ids
Related