KIVY:AttributeError:'NoneType'对象没有属性'text'

时间:2016-11-11 06:47:51

标签: python python-3.x kivy kivy-language

我是Kivy的菜鸟,所以我现在的问题如下: 我一直有这个属性错误,我知道我错过了什么,所以在继续我的应用程序之前我需要先理解这个:

main.kv:

MainRoot:

<MainRoot>:
    padding: 10
    spacing: 10
    canvas:
        Color:
            rgb: .2, .2, .2
        Rectangle:
            pos: self.pos
            size: self.size



    ClientArea:
    ProductArea:
    DisplayArea:

<ProductArea>:
    text_input: text_input_view
#    search_results: search_results_view
    minimum_width: '480dp'
    orientation:'vertical'
    canvas:
        Color:
            rgb: .25, .25, .25
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:
        orientation: 'horizontal'
        size_hint_y: None
        height: '30dp'
        TextInput:
            id: text_input_view
            size_hint_x: 90
            focus: True
            multiline: False
            on_text: root.search()
        Button:
            text: 'Search'
            size_hint_x: 25
            on_press: root.search()
    ListView:
#        id: search_results_view
#        adapter: root.dict_adapter

    BoxLayout:
        orientation: 'horizontal'
        size_hint_y: None
        height: '50dp'


        Button:
            size_hint_x: 10
            text: '-5'
        Button:
            size_hint_x: 15
            text: '-'
        Button:
            size_hint_x: 50
            text: 'Add to cart'

        Button:
            size_hint_x: 15
            text: '+'

        Button:
            size_hint_x: 10
            text: '+5'

    ListView:


<ClientArea>:

    orientation:'vertical'
    canvas:
        Color:
            rgb: .22, .22, .22
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:
        orientation: 'horizontal'
        size_hint_y: None
        height: '30dp'
        TextInput:
            id: search_client_view
            size_hint_x: 90
            focus: True
            multiline: False
        Button:
            text: 'Search'
            size_hint_x: 25
    ListView:

    BoxLayout:
        orientation: 'horizontal'
        size_hint_y: None
        height: '30dp'
        Button:
            text: 'Select'

    BoxLayout:
        Label:
            text: 'Client Display'


<DisplayArea>:
    orientation:'vertical'
    canvas:
        Color:
            rgb: .22, .22, .22
        Rectangle:
            pos: self.pos
            size: self.size
    BoxLayout:
        canvas:
            Color:
                rgb: .4, .4, .4
            Rectangle:
                pos: self.pos
                size: self.size
    BoxLayout:
        canvas:
            Color:
                rgb: .35, .35, .35
            Rectangle:
                pos: self.pos
                size: self.size
    BoxLayout:
        canvas:
            Color:
                rgb: .4, .4, .4
            Rectangle:
                pos: self.pos
                size: self.size

和main.py:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty, ListProperty, DictProperty, BooleanProperty, StringProperty
from kivy.uix.label import Label
from kivy.adapters.listadapter import ListAdapter
from kivy.adapters.dictadapter import DictAdapter
from kivy.uix.textinput import TextInput

from kivy.uix.listview import ListItemButton, CompositeListItem, ListItemLabel
from kivy.config import Config

from db_handlers import DbQueries as dbq

Config.set('graphics', 'height', '900')
Config.set('graphics', 'width', '1200')
Config.set('graphics', 'minimum_height', '940')
Config.set('graphics', 'minimum_width', '1400')


class ClientArea(BoxLayout):
    pass


class ProductArea(BoxLayout):
    #search display properties and variables
    text_input = ObjectProperty()
    search_results = ObjectProperty()
    #cart properties and variables
    dict_adapter = ObjectProperty({'': ''})

    def __init__(self, **kwargs):

        super(ProductArea, self).__init__(**kwargs)
        self.search(self.text_input)

    def search(self, txt_input):

        print('Hello' + txt_input.text)


class ProductListButton(ListItemButton):
    def __init__(self, **kwargs):
        kwargs['size_hint_y'] = None
        kwargs['height'] = '55dp'
        kwargs['text_size'] = (self.width*3, None)
        kwargs['deselected_color'] = [.4, .4, .4, 1]
        kwargs['selected_color'] = [.7, .9, .7, 1]

        super(ProductListButton, self).__init__(**kwargs)


class CartButton(CompositeListItem):
    def __init__(self, **kwargs):
        kwargs['size_hint_y'] = None
        kwargs['height'] = '60dp'
        super(CartButton, self).__init__(**kwargs)


class ProductButton(ListItemButton):
    def __init__(self, **kwargs):
        kwargs['size_hint'] = (1, 1)
        kwargs['height'] = self.height*2
        kwargs['text_size'] = (self.width*2, None)
        kwargs['deselected_color'] = [.8, .8, .8, 1]
        kwargs['selected_color'] = [.4, .4, .4, 1]
        super(ProductButton, self).__init__(**kwargs)


class PriceButton(ListItemButton):
    def __init__(self, **kwargs):
        kwargs['size_hint'] = (None, 1)
        kwargs['width'] = "80dp"
        kwargs['deselected_color'] = [.8, .8, .8, 1]
        kwargs['selected_color'] = [.4, .4, .4, 1]
        super(PriceButton, self).__init__(**kwargs)


class QuantityButton(ListItemButton):
    def __init__(self, **kwargs):
        kwargs['size_hint'] = (None, 1)
        kwargs['width'] = "50dp"
        kwargs['deselected_color'] = [.8, .8, .8, 1]
        kwargs['selected_color'] = [.4, .4, .4, 1]
        super(QuantityButton, self).__init__(**kwargs)


class AddQuantityButton(ListItemButton):
    def __init__(self, **kwargs):
        kwargs['size_hint'] = (None, 1)
        kwargs['width'] = "40dp"
        kwargs['deselected_color'] = [.7, .9, .7, 1]
        kwargs['selected_color'] = [.7, .9, .7, 1]
        super(AddQuantityButton, self).__init__(**kwargs)


class SubtractQuantityButton(ListItemButton):
    def __init__(self, **kwargs):
        kwargs['size_hint'] = (None, 1)
        kwargs['width'] = "40dp"
        kwargs['deselected_color'] = [.9, .7, .7, 1]
        kwargs['selected_color'] = [.9, .7, .7, 1]
        super(SubtractQuantityButton, self).__init__(**kwargs)


class DisplayArea(BoxLayout):
    pass


class MainRoot(GridLayout):

    def __init__(self, **kwargs):
        kwargs['cols'] = 3
        super(MainRoot, self).__init__(**kwargs)


class MainApp(App):
    def build(self):
        return MainRoot()

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

错误:

    File "/Users/mac/Desktop/Pharma_dev/v002/main.py", line 35, in __init__
     self.search(self.text_input)
   File "/Users/mac/Desktop/Pharma_dev/v002/main.py", line 39, in search
     print('Hello' + txt_input.text)
 AttributeError: 'NoneType' object has no attribute 'text'

0 个答案:

没有答案