繁体   English   中英

Python / kivy:AttributeError:“ int”对象在python中没有属性“ replace”

[英]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'

使用NumericProperty ,然后在kv中使用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()

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

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM