Python / Kivy - TypeError:' int'对象不可迭代

时间:2016-11-19 04:30:35

标签: python kivy

我试图在屏幕上放置一个标签,当我这样做时,但当我尝试格式化文本时,使用Label" text_size"我收到此错误:" TypeError:' int'对象不可迭代"

以下是代码:

#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()

这是我尝试的不同方式:

#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()

在这两种方式中我都得到同样的错误:

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

1 个答案:

答案 0 :(得分:2)

尝试面向对象的方法,构建一个从kivy的App类继承的类,为它提供构建方法,让它返回应用程序的根小部件:

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")
"""