Python/kivy : AttributeError: 'int' object has no attribute 'replace' in python

Python/kivy : AttributeError: 'int' object has no attribute 'replace' in python

我有 2 个文件 test.py 和 test.kv。当我 运行 test.py 并在 self.abc.text=10 中传递数值时,它会给出错误
文件“/usr/lib/python2.7/dist-packages/kivy/uix/textinput.py”,第 2930 行,在 _set_text 中 text = text.replace(u'\r\n', u'\n') AttributeError: 'int' 对象没有属性 'replace'

如果我传递字符串值,那么它就可以工作。我认为文本是字符串值,但我不知道数字值是什么?

test.py

import kivy

kivy.require('1.9.0')  # replace with your current kivy version !
import sqlite3 as lite
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty
from kivy.lang import Builder

from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.uix.label import Label
#Window.maximize()
from kivy.clock import Clock
from kivy.uix.treeview import TreeView, TreeViewLabel, TreeViewNode

Window.size = (500, 530)


class GroupScreen(Screen):
    groupName = ObjectProperty(None)
    popup = ObjectProperty(None)
    abc = ObjectProperty(None)

    def display_groups(self, instance):
        self.abc.text=10



class Group(App):


    def build(self):
        self.root = Builder.load_file('test.kv')
        return self.root


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

test.kv

#:kivy 1.10.0

<CustomLabel@Label>:
    text_size: self.size
    valign: "middle"
    padding_x: 5

<SingleLineTextInput@TextInput>:
    multiline: False

<GreenButton@Button>:
    background_color: 1, 1, 1, 1
    size_hint_y: None
    height: self.parent.height * 0.150

GroupScreen:
    groupName: groupName
    abc:abc
    GridLayout:
        cols: 2
        padding : 30,30
        spacing: 10, 10
        row_default_height: '40dp'

        CustomLabel:
            text: 'Number'

        SingleLineTextInput:
            id: abc

        CustomLabel:
            text: 'Test'

        SingleLineTextInput:
            id: groupName
            on_text: root.display_groups(self)

        GreenButton:
            text: 'Ok'

        GreenButton:
            text: 'Cancel'

您需要输入 self.abc.text = str(rows[1]) 才能将其作为正确的类型传递。

希望对您有所帮助!

在kv中使用NumericProperty然后str(root.abc)
试试这个例子:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty


class MyBoxLayout(BoxLayout):

    abc = NumericProperty(0)

    def set_text(self):
        self.abc = 42



KV = """

MyBoxLayout:

    Button:
        text: str(root.abc)
        on_release:
            root.set_text()

"""


class Testapp(App):
    def build(self):
        root = Builder.load_string(KV)
        return root


Testapp().run()