How can I dispaly the selected item from recycleview to the texinput

天涯浪子 提交于 2021-01-05 07:28:21

问题


In following code I want to dispaly the selected item from recycleview to the text_box1 which is from class MyTextInput and the code to display in textbox is in the class SelectableLabel --> method apply_selection.

the when I print the selected item it gets printed on console. when i try to display it in the textinput it shows the error. What changes should I do. the solution with the code will be good to understand. thank you

test.py file

from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import NumericProperty, ListProperty, BooleanProperty, ObjectProperty,StringProperty
from kivy.uix.recycleview import RecycleView
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
import pandas as pd

Builder.load_string('''
<Body>:
    canvas:
        Color:
            rgba:(1, 1, 1, 1)
        Rectangle:
            # pos: self.pos
            size: self.size


<DropDownWidget>:
    
    canvas:
        Color:
            rgba:(1, 1, 1, 1)
        Rectangle:
            # pos: self.pos
            size: self.size

    # orientation: 'vertical'
    spacing: 20
    txt_input: txt_input
    rv: rv
    # txt_input1: txt_input1
    MyTextInput:
        id: txt_input1
        pos: 400,300
        size_hint_y: None
        height: 50
    MyTextInput:
        id: txt_input
        hint_text:'Enter here'
        size_hint_y: None
        height: 50
    RV:
        id: rv
        
    

<MyTextInput>:
    
    readonly: False
    multiline: False

<SelectableLabel>:
    
    
    # Draw a background to indicate selection
    color: 0,0,0,1
    canvas.before:
        Color:
            rgba: (0, 0, 1, .5) if self.selected else (1, 1, 1, 1)
        Rectangle:
            # pos: self.pos
            size: self.size
    
<RV>:
    canvas:
        Color:
            rgba: 0,0,0,.2

        Line:
            rectangle: self.x +1 , self.y, self.width - 2, self.height -2
        

    bar_width: 10
    scroll_type:['bars']
    viewclass: 'SelectableLabel'
    SelectableRecycleBoxLayout:
        default_size: None, dp(20)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        multiselect: False
        
        ''')


class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
                                 RecycleBoxLayout):

    ''' Adds selection and focus behaviour to the view. '''


class SelectableLabel(RecycleDataViewBehavior, Label):
    ''' Add selection support to the Label '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)
    txt_input1 = ObjectProperty(None)
    txt_input = ObjectProperty(None)

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(SelectableLabel, self).refresh_view_attrs(
            rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableLabel, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''


        self.selected = is_selected
        if is_selected:
            self.ids.MyTextInput.txt_input1.text = str(rv.data[index])
            print("selection changed to {0}".format(rv.data[index]))


class RV(RecycleView):
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)


class DropDownWidget(BoxLayout):
    txt_input = ObjectProperty()
    rv = ObjectProperty()
    txt_input1 = ObjectProperty()



class MyTextInput(TextInput):

    txt_input = ObjectProperty()
    txt_input1 = ObjectProperty(None)
    flt_list = ObjectProperty()
    word_list = ListProperty()
    # this is the variable storing the number to which the look-up will start
    starting_no = NumericProperty(3)
    suggestion_text = ''

    def __init__(self, **kwargs):
        super(MyTextInput, self).__init__(**kwargs)

    def on_text(self, instance, value):
        # find all the occurrence of the word
        self.parent.ids.rv.data = []
        matches = [self.word_list[i] for i in range(len(self.word_list)) if
                   self.word_list[i][:self.starting_no] == value[:self.starting_no]]
        # display the data in the recycleview
        display_data = []
        for i in matches:
            display_data.append({'text': i})
        self.parent.ids.rv.data = display_data
        # ensure the size is okay
        if len(matches) <= 10:
            self.parent.height = (50 + (len(matches) * 20))
        else:
            self.parent.height = 240

    def keyboard_on_key_down(self, window, keycode, text, modifiers):
        if self.suggestion_text and keycode[1] == 'tab':
            self.insert_text(self.suggestion_text + ' ')
            return True
        return super(MyTextInput, self).keyboard_on_key_down(window, keycode, text, modifiers)


class Body(FloatLayout):
    def __init__(self, **kwargs):
        f = pd.read_csv("stoploss.csv")
        fl = len(f.index)
        file = pd.DataFrame(f, columns=['Stock Symbol', 'Purchase Price', 'Stock Name', 'Stop Loss(%)'])
        j = 0
        wl = []
        for i in range(fl):
            for index in range(1):
                columnSeriesObj = file.iloc[:, 2]
                # pp = iter(columnSeriesObj.values)
                # pp1 = next(pp)
                # print(pp1)

                wl.append(columnSeriesObj.values[i])

        tp = tuple(wl)
        print(str(tp))

        # def convertTuple(tup):
        #     str = ''.join(tup)
        #     return str
        # print(convertTuple(tp))
        super(Body, self).__init__(**kwargs)
        widget_1 = DropDownWidget(pos_hint={'center_x': .5, 'center_y': .5},
                                  size_hint=(None, None), size=(600, 60))
        widget_1.ids.txt_input.word_list = wl
        widget_1.ids.txt_input.starting_no = 3

        self.add_widget(widget_1)


class MyApp(App):

    def build(self):
        return Body()



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

But I am getting the following error:

"C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\python.exe" D:/FirebaseLoginScreen-master/try_code.py
[INFO   ] [Logger      ] Record log in C:\Users\Rushi Dada\.kivy\logs\kivy_20-12-12_86.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.gstreamer" 0.3.1
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.1.10
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.1.12
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.1.23
[INFO   ] [Kivy        ] v1.11.1
[INFO   ] [Kivy        ] Installed at "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
[INFO   ] [Python      ] Interpreter at "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\python.exe"
[INFO   ] [Factory     ] 184 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
('Tesla inc', 'Tata Motors Limited ', 'asv', 'tesla', 'ploul', 'fd', 's', 'asdsd', 'trtdfsddfdfd', 'abc')
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.6.0 NVIDIA 391.35'>
[INFO   ] [GL          ] OpenGL vendor <b'NVIDIA Corporation'>
[INFO   ] [GL          ] OpenGL renderer <b'GeForce GT 635M/PCIe/SSE2'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 6
[INFO   ] [GL          ] Shading version <b'4.60 NVIDIA'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [GL          ] NPOT texture support is available
[INFO   ] [Base        ] Start application main loop
[INFO   ] [Base        ] Leaving application in progress...
 Traceback (most recent call last):
   File "kivy\properties.pyx", line 860, in kivy.properties.ObservableDict.__getattr__
 KeyError: 'MyTextInput'
 
 During handling of the above exception, another exception occurred:
 
 Traceback (most recent call last):
   File "D:/FirebaseLoginScreen-master/try_code.py", line 291, in <module>
     MyApp().run()
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\app.py", line 855, in run
     runTouchApp()
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\base.py", line 504, in runTouchApp
     EventLoop.window.mainloop()
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\core\window\window_sdl2.py", line 747, in mainloop
     self._mainloop()
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\core\window\window_sdl2.py", line 479, in _mainloop
     EventLoop.idle()
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\base.py", line 342, in idle
     self.dispatch_input()
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\base.py", line 327, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\base.py", line 233, in post_dispatch_input
     listener.dispatch('on_motion', etype, me)
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\core\window\__init__.py", line 1402, in on_motion
     self.dispatch('on_touch_down', me)
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\core\window\__init__.py", line 1418, in on_touch_down
     if w.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\widget.py", line 549, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\widget.py", line 549, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\scrollview.py", line 647, in on_touch_down
     if self.dispatch('on_scroll_start', touch):
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\scrollview.py", line 736, in on_scroll_start
     return self.simulate_touch_down(touch)
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\scrollview.py", line 642, in simulate_touch_down
     ret = super(ScrollView, self).on_touch_down(touch)
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\widget.py", line 549, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\behaviors\focus.py", line 443, in on_touch_down
     return super(FocusBehavior, self).on_touch_down(touch)
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\widget.py", line 549, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "D:/FirebaseLoginScreen-master/try_code.py", line 192, in on_touch_down
     return self.parent.select_with_touch(self.index, touch)
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\behaviors\compoundselection.py", line 345, in select_with_touch
     self.select_node(node)
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\recycleview\layout.py", line 103, in select_node
     self.apply_selection(node, view, True)
   File "C:\Users\Rushi Dada\AppData\Local\Programs\Python\Python37\lib\site-packages\kivy\uix\recycleview\layout.py", line 143, in apply_selection
     view.apply_selection(self.recycleview, index, is_selected)
   File "D:/FirebaseLoginScreen-master/try_code.py", line 200, in apply_selection
     self.ids.MyTextInput.txt_input1.text = str(rv.data[index])
   File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

Process finished with exit code 1

回答1:


You are trying to access a MyTextInput instance using

self.ids.MyTextInput.txt_input1.text = str(rv.data[index])

in the SelectableLabel class, but the SelectableLabel instance has no ids at all, so that won't work. And there is no MyTextInput id defined anywhere in your code. I am guessing that you intend to set the text of one of the MyTextInput instances that appear in your DropDownWidget.

In order to access the DropDownWidget that you create in the Body class, you can just save a reference to it in the __init__() method by making the following slight modifications:

    super(Body, self).__init__(**kwargs)
    self.widget_1 = DropDownWidget(pos_hint={'center_x': .5, 'center_y': .5},
                              size_hint=(None, None), size=(600, 60))
    self.widget_1.ids.txt_input.word_list = wl
    self.widget_1.ids.txt_input.starting_no = 3

    self.add_widget(self.widget_1)

Then, in the apply_selection() method of SelectableLabel, you can adjust the access of the MyTextInput like this:

def apply_selection(self, rv, index, is_selected):
    ''' Respond to the selection of items in the view. '''


    self.selected = is_selected
    if is_selected:
        # self.ids.MyTextInput.txt_input1.text = str(rv.data[index])
        App.get_running_app().root.widget_1.ids.txt_input1.text = str(rv.data[index])
        print("selection changed to {0}".format(rv.data[index]))


来源:https://stackoverflow.com/questions/65267709/how-can-i-dispaly-the-selected-item-from-recycleview-to-the-texinput

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!