AttributeError setting the label text - gio123 - Mar-14-2018
Hi All,
I receive this error message when I try to set an input text value to a label text.
self.b5.text="You wrote " + self.username.txt
AttributeError: 'LoginScreen' object has no attribute 'b5'
This is the Python code.
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class LoginScreen(GridLayout):
def buttonClicked(self, instance):
self.b4.text="You wrote " + self.username.text
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.cols = 1
self.add_widget(Label(text="Hello"))
self.username = TextInput(multiline=True)
self.add_widget(self.username)
b1=(Button(text="Find"))
self.add_widget(b1)
b1.bind(on_press=self.buttonClicked)
b4=(Label(text="Answer:"))
self.add_widget(b4)
b5=(Label(text=''))
self.add_widget(b5)
class SimpleKivy(App):
def build(self):
return LoginScreen()
if __name__ == "__main__":
SimpleKivy().run() Any help will be greatly appreciated.
Thanks in advance for your kind support.
Regards,
Giovanni
RE: AttributeError setting the label text - buran - Mar-14-2018
Post your actual code that produce this error as well the full traceback in error tags.
Note the discrepancy at the moment - line 10 (self.b4) vs. your error msg (self.b5)
RE: AttributeError setting the label text - gio123 - Mar-15-2018
Hi Buran,
this is the actual code. This code is in Android 5.1.1 environment with QPyton (2.7.12).
#-*-coding:utf8;-*-
#qpy:2
#qpy:kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class LoginScreen(GridLayout):
def buttonClicked(self,instance):
self.b5.text='You wrote ' + self.username.text
def clsInputText(self,instance):
self.username.text=''
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.cols = 1
self.add_widget(Label(text="SAP Support Team Finder"))
self.username = TextInput(multiline=True)
self.add_widget(self.username)
b1=(Button(text='Find'))
self.add_widget(b1)
b1.bind(on_press=self.buttonClicked)
b2=(Button(text='Clear'))
self.add_widget(b2)
b2.bind(on_press=self.clsInputText)
b3=(Button(text='Exit'))
self.add_widget(b3)
b3.bind(on_press=exit)
b4=(Label(text='Answer:'))
self.add_widget(b4)
b5=(Label(text=''))
self.add_widget(b5)
class SimpleKivy(App):
def build(self):
return LoginScreen()
if __name__ == "__main__":
SimpleKivy().run() When I enter a text in the textbox and I press the [Find] button I receive an error. This is the complete traceback.
Error: [INFO ] [Kivy ] v1.10.0
[INFO ] [Python ] v2.7.12 (default, Nov 21 2017, 21:49:35)
[GCC 4.9.x 20150123 (prerelease)]
[INFO ] [Factory ] 194 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_pygame, img_pil, img_gif (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: pygame
[INFO ] [Window ] Provider: pygame
[INFO ] [GL ] Using the "OpenGL ES 2" graphics system
[INFO ] [GL ] Backend used <gl>
[INFO ] [GL ] OpenGL version <OpenGL ES 3.0 [email protected] AU@ (GIT@I32dcf20b99)>
[INFO ] [GL ] OpenGL vendor <Qualcomm>
[INFO ] [GL ] OpenGL renderer <Adreno (TM) 306>
[INFO ] [GL ] OpenGL parsed version: 3, 0
[INFO ] [GL ] Texture max size <4096>
[INFO ] [GL ] Texture max units <16>
Traceback (most recent call last):
File "/data/data/org.qpython.qpy/files/lib/python27.zip/logging/__init__.py", line 882, in emit
stream.write(fs % msg)
File "<string>", line 23, in write
TypeError: argument 1 must be string without null bytes, not str
Logged from file __init__.py, line 1051
[ERROR ] [Shader ] GL error 1281
[INFO ] [Support ] Android install hooks
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [OSC ] using <multiprocessing> for socket
[INFO ] [Base ] Start application main loop
[INFO ] [Android ] found 17 joystick
[INFO ] [Android ] create joystick <0>
[INFO ] [Android ] discard joystick <0> cause no button
[INFO ] [Android ] create joystick <1>
[INFO ] [Android ] create joystick <2>
[INFO ] [Android ] create joystick <3>
[INFO ] [Android ] create joystick <4>
[INFO ] [Android ] create joystick <5>
[INFO ] [Android ] create joystick <6>
[INFO ] [Android ] create joystick <7>
[INFO ] [Android ] create joystick <8>
[INFO ] [Android ] create joystick <9>
[INFO ] [Android ] create joystick <10>
[INFO ] [Android ] create joystick <11>
[INFO ] [Android ] create joystick <12>
[INFO ] [Android ] create joystick <13>
[INFO ] [Android ] create joystick <14>
[INFO ] [Android ] create joystick <15>
[INFO ] [Android ] create joystick <16>
[INFO ] [GL ] NPOT texture support is available
[ERROR ] [Base ] Failed to import "android" module. Could not remove android presplash.
[INFO ] [Base ] Leaving application in progress...
Traceback (most recent call last):
File "main.py", line 68, in <module>
SimpleKivy().run()
File "/data/data/org.qpython.qpy/files/lib/python2.7/site-packages/kivy_u4_qpython-1.10.0-py2.7.egg/kivy/app.py", line 828, in run
runTouchApp()
File "/data/data/org.qpython.qpy/files/lib/python2.7/site-packages/kivy_u4_qpython-1.10.0-py2.7.egg/kivy/base.py", line 504, in runTouchApp
EventLoop.window.mainloop()
File "/data/data/org.qpython.qpy/files/lib/python2.7/site-packages/kivy_u4_qpython-1.10.0-py2.7.egg/kivy/core/window/window_pygame.py", line 403, in mainloop
self._mainloop()
File "/data/data/org.qpython.qpy/files/lib/python2.7/site-packages/kivy_u4_qpython-1.10.0-py2.7.egg/kivy/core/window/window_pygame.py", line 289, in _mainloop
EventLoop.idle()
File "/data/data/org.qpython.qpy/files/lib/python2.7/site-packages/kivy_u4_qpython-1.10.0-py2.7.egg/kivy/base.py", line 342, in idle
self.dispatch_input()
File "/data/data/org.qpython.qpy/files/lib/python2.7/site-packages/kivy_u4_qpython-1.10.0-py2.7.egg/kivy/base.py", line 327, in dispatch_input
post_dispatch_input(*pop(0))
File "/data/data/org.qpython.qpy/files/lib/python2.7/site-packages/kivy_u4_qpython-1.10.0-py2.7.egg/kivy/base.py", line 233, in post_dispatch_input
listener.dispatch('on_motion', etype, me)
File "kivy/_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7726)
File "/data/data/org.qpython.qpy/files/lib/python2.7/site-packages/kivy_u4_qpython-1.10.0-py2.7.egg/kivy/core/window/__init__.py", line 1188, in on_motion
self.dispatch('on_touch_down', me)
File "kivy/_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7726)
File "/data/data/org.qpython.qpy/files/lib/python2.7/site-packages/kivy_u4_qpython-1.10.0-py2.7.egg/kivy/core/window/__init__.py", line 1204, in on_touch_down
if w.dispatch('on_touch_down', touch):
File "kivy/_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7726)
File "/data/data/org.qpython.qpy/files/lib/python2.7/site-packages/kivy_u4_qpython-1.10.0-py2.7.egg/kivy/uix/widget.py", line 457, in on_touch_down
if child.dispatch('on_touch_down', touch):
File "kivy/_event.pyx", line 718, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7726)
File "/data/data/org.qpython.qpy/files/lib/python2.7/site-packages/kivy_u4_qpython-1.10.0-py2.7.egg/kivy/uix/behaviors/button.py", line 151, in on_touch_down
self.dispatch('on_press')
File "kivy/_event.pyx", line 714, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:7681)
File "kivy/_event.pyx", line 1225, in kivy._event.EventObservers.dispatch (kivy/_event.c:13524)
File "kivy/_event.pyx", line 1149, in kivy._event.EventObservers._dispatch (kivy/_event.c:13050)
File "main.py", line 19, in buttonClicked
self.b5.text='You wrote ' + self.username.text
AttributeError: 'LoginScreen' object has no attribute 'b5'
Thanks in advance for your kind support.
Regards,
Giovanni
RE: AttributeError setting the label text - buran - Mar-15-2018
you need to add self. in front of every b widget, e.g. self.b5, everywhere you use it if you want to be class properties. without self it's just a name that exists in __init__() only
|