Python / Kivy - TypeError: 'int' object is not iterable

I'm trying to put a label in the screen, when I just do this works, but when I try to format the text, with Label's "text_size" I get this error: "TypeError: 'int' object is not iterable"

Here is the code:

#coding: utf-8

from kivy.app import App
from kivy.uix.label import Label

def build():
    return Label(text = "Hello world!", italic=True, text_size=50)

app = App()
app.build = build
app.run()

Here's the different way that I tried:

#coding: utf-8

from kivy.app import App
from kivy.uix.label import Label

def build():
    lb = Label()
    lb.text= "Hello world!"
    lb.italic = True
    lb.text_size = 50
    return lb

app = App()
app.build = build
app.run()

In the both ways I get this same error:

C:\Users\jaumh\Anaconda3\envs\k35\python.exe C:/dev/kivy/Source/0001_hello_world/main.py
[INFO              ] [Logger      ] Record log in C:\Users\jaumh\.kivy\logs\kivy_16-11-19_14.txt
[INFO              ] [Kivy        ] v1.9.1
[INFO              ] [Python      ] v3.5.2 |Continuum Analytics, Inc.| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]
[INFO              ] [Factory     ] 179 symbols loaded
[INFO              ] [Image       ] Providers: img_tex, img_dds, img_gif, img_sdl2 (img_pil, img_ffpyplayer ignored)
[INFO              ] [Text        ] Provider: sdl2
[INFO              ] [OSC         ] using <thread> for socket
[INFO              ] [Window      ] Provider: sdl2
[INFO              ] [GL          ] GLEW initialization succeeded
[INFO              ] [GL          ] OpenGL version <b'4.4.0 - Build 20.19.15.4300'>
[INFO              ] [GL          ] OpenGL vendor <b'Intel'>
[INFO              ] [GL          ] OpenGL renderer <b'Intel(R) HD Graphics 5500'>
[INFO              ] [GL          ] OpenGL parsed version: 4, 4
[INFO              ] [GL          ] Shading version <b'4.40 - Build 20.19.15.4300'>
[INFO              ] [GL          ] Texture max size <16384>
[INFO              ] [GL          ] Texture max units <32>
[INFO              ] [Shader      ] fragment shader: <b"WARNING: 0:7: '' :  #version directive missing">
[INFO              ] [Shader      ] vertex shader: <b"WARNING: 0:7: '' :  #version directive missing">
[INFO              ] [Window      ] auto add sdl2 input provider
[INFO              ] [Window      ] virtual keyboard not allowed, single mode, not docked
 Traceback (most recent call last):
   File "C:/dev/kivy/Source/0001_hello_world/main.py", line 11, in <module>
     hello_world.run()
   File "C:\Users\jaumh\Anaconda3\envs\k35\lib\site-packages\kivy\app.py", line 802, in run
     root = self.build()
   File "C:/dev/kivy/Source/0001_hello_world/main.py", line 7, in build
     return Label(text = "Hello world!", italic=True, text_size=50)
   File "C:\Users\jaumh\Anaconda3\envs\k35\lib\site-packages\kivy\uix\label.py", line 266, in __init__
     super(Label, self).__init__(**kwargs)
   File "C:\Users\jaumh\Anaconda3\envs\k35\lib\site-packages\kivy\uix\widget.py", line 312, in __init__
     super(Widget, self).__init__(**kwargs)
   File "kivy\_event.pyx", line 273, in kivy._event.EventDispatcher.__init__ (kivy\_event.c:5348)
   File "kivy\properties.pyx", line 408, in kivy.properties.Property.__set__ (kivy\properties.c:5114)
   File "kivy\properties.pyx", line 732, in kivy.properties.ListProperty.set (kivy\properties.c:11113)
   File "kivy\properties.pyx", line 620, in kivy.properties.ObservableList.__init__ (kivy\properties.c:8397)
 TypeError: 'int' object is not iterable

Process finished with exit code 1

Upvotes: 1

Views: 1375

Answers (1)

nick
nick

Reputation: 1207

Try an object oriented approach, build a class that inherit's from kivy's App class, provide it with a build method, make it return the root widget for the application:

from kivy.app import App
from kivy.uix.label import Label

class MyApp(App):

    def build(self):
        return Label(text='Hello World')

if __name__ == '__main__':
    MyApp().run()

"""
# Alternatively 

class MyLabel(Label):
    def __init__(self,text):
        super(MyLabel, self).__init__()
        self.text = text
        self.italic = True
        self.font_size = 50

class MyApp(App):
    def build(self):
        return MyLabel("Hello Mars")
"""

Upvotes: 3

Related Questions