我如何在同一屏幕上显示列表视图以及如何制作会提供建议的搜索框

How can i show list view in same screen and also how can I make search box which will give suggestions

代码如下。我无法在屏幕上显示 class ListApp(MDApp)(列表视图)class。请告诉我我做错了什么?

我还想为股票创建一个搜索框(在 class stockinput(Screen)),它将接受来自使用的输入,并且在键入时将通过从 . csv 文件(该文件包含股票的详细信息,如股票名称、股票代码等),在选择股票名称后,它将自动填充为股票详细信息提供的文本框

run1.py 文件

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from Option import OptionApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from datetime import datetime
import pandas_datareader.data as web
import pandas as pd
from kivymd.uix.screen import Screen
from kivymd.uix.list import MDList,ThreeLineListItem,ThreeLineAvatarIconListItem
from kivymd.uix.list import IconLeftWidget,ImageLeftWidget
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from kivymd.app import MDApp
from kivy.app import App
from kivy.properties import ObjectProperty
import csv
from os import path
kv = Builder.load_file("run1.kv")

class signin(Screen):

    user_name = ObjectProperty(None)

    def btn(self):
        username = self.user_name.text
        print(username)
        sm.current = 'option_screen'





class option(Screen):
    def btn_addstock(self):
        sm.current = 'stockinput_screen'

    def btn_stoplosslist(self):
        ListApp().run()







class stockinput(Screen):
    stock_name = ObjectProperty(None)
    stock_symbol = ObjectProperty(None)
    purchase_price = ObjectProperty(None)
    stop_loss = ObjectProperty(None)


    def btn(self):
        end = datetime.today().date()
        start = end.year - 10
        start = datetime(start, datetime.today().month, datetime.today().day).date()
        uname = input("Enter user name: ")
        print("Stock Name:", self.stock_name.text, "Stock Symbol:", self.stock_symbol.text)
        print("Purchase Price:",self.purchase_price.text,"Stop Loss(%):",self.stop_loss.text)

        #write data to csv file

        # if path.exists("stoploss.csv"):
        #     myFile = open('stoploss.csv', 'a')
        # else:
        #     myFile = open('stoploss.csv', 'w')
        file_name = stockinput.uname + "_stoploss.csv"
        if path.exists(file_name):
            with open(file_name, "a+", newline='')as newFile :
                fieldnames = ["Stock Name", "Stock Symbol", "Purchase Price", "Stop Loss(%)"]
                newFileWriter = csv.DictWriter(newFile, fieldnames=fieldnames)
                newFileWriter.writerow({"Stock Name" : self.stock_name.text,"Stock Symbol" : self.stock_symbol.text,"Purchase Price" : self.purchase_price.text,"Stop Loss(%)" : self.stop_loss.text})

        else:
            myFile = open(file_name, 'w+')
            myData = [["Stock Name", "Stock Symbol", "Purchase Price", "Stop Loss(%)"],[self.stock_name.text, self.stock_symbol.text, self.purchase_price.text, self.stop_loss.text]]

            with myFile:
                writer = csv.writer(myFile)
                writer.writerows(myData)

        df = web.DataReader(self.stock_symbol.text, 'yahoo', start, end,)
        print(df.tail())

        self.stock_name.text = ""
        self.stock_symbol.text = ""
        self.purchase_price.text = ""
        self.stop_loss.text = ""



f = pd.read_csv("stoploss.csv")
file = pd.DataFrame(f, columns=['Stock Symbol','Purchase Price','Stock Name','Stop Loss(%)'])



class ListApp(MDApp):
    def Wstop(self,event):
        ListApp().stop()



    def build(self):

        end = datetime(2020, 12, 10)
        start = datetime(2020, 12, 10)
        btn = Button(text="Back",
                     font_size="20sp",
                     background_color=(1, 1, 1, 1),
                     color=(1, 1, 1, 1),
                     size=(12, 12),
                     size_hint=(.1, .05),
                     pos=(600, 500), on_press=self.Wstop)
        screen = Screen()



        scroll = ScrollView()
        list_view = MDList()


        scroll.add_widget(list_view)



        i = 0
        fl = len(file.index)
        try:
            for index in range(fl):

                for index in range(1):
                    columnSeriesObj2 = file.iloc[:, 0]

                    df = web.DataReader(columnSeriesObj2.values[i],'yahoo', start, end,retry_count=3)
                    print(df.head())
                    Objname = file.iloc[:, 2]
                    columnSeriesObj = df.iloc[:, 3]
                    columnSeriesObj1 = file.iloc[:, 1]
                    ObjStoploss = file.iloc[:, 3]

                    cp = iter(columnSeriesObj.values)
                    pp = iter(columnSeriesObj1.values)
                    pp1 = next(pp)
                    cp1 = columnSeriesObj.values[0]


                    sl = columnSeriesObj1.values[i] - (columnSeriesObj1.values[i] * (ObjStoploss.values[i]/100))


                    if cp1 <= sl:
                        image = ImageLeftWidget(source='loss.png')
                        items = ThreeLineAvatarIconListItem(text="Alert sale " + Objname.values[i], secondary_text='Close price: '+str(cp1),
                                                            tertiary_text='Stoploss: ' + str(sl))
                        items.add_widget(image)
                        list_view.add_widget(items)



                        i=i+1


                    else:
                        image = ImageLeftWidget(source='profit.jpg')
                        items = ThreeLineAvatarIconListItem(text="Chill " + Objname.values[i],
                                                            secondary_text='Close price: ' + str(cp1),
                                                            tertiary_text='Stoploss: ' + str(sl))
                        items.add_widget(image)
                        list_view.add_widget(items)


                        i=i+1
        except ConnectionAbortedError:
            print("Check your Internet connection")
        except ConnectionRefusedError:
            print("Check your Internet connection")
        except ConnectionError:
            print("Check your Internet connection")
        except ConnectionResetError:
            print("Check your Internet connection")
        except TimeoutError:
            print("Timeout!!!!...Check your Internet connection")
        # except KeyError:
        #     pass

        # except:
        #     print("Something went wrong")
        print("Done")

        screen.add_widget(scroll)
        screen.add_widget(btn)
        return screen

class WindowsManager(ScreenManager):
    pass

sm = ScreenManager()
sm.add_widget(signin(name='signin_screen'))
sm.add_widget(option(name='option_screen'))
sm.add_widget(stockinput(name='stockinput_screen'))
sm.add_widget(ListApp(name='Stoploss_ip'))

class run1(MDApp):
    def build(self):
        return sm

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

run1.kv 文件

<WindowsManager>:
    signin:
    option:
    stockinput:
    ListApp:


<signin>:

    name: 'signin_screen'

    user_name: user_name



    FloatLayout:

        TextInput:
            id: user_name
            multiline:False
            size_hint: 2.0 ,.1
            pos: 120,280
            hint_text: "Email_ID"

        Button:
            text:"Submit"
            size_hint: .5 ,.3
            pos: 150,100
            on_press : root.btn()
#            app.root.current = 'option_screen'

#                app.stop()
#                root.btn()




        Label:
            text:"Please Do not change the Email_ID. Data will be saved as per your Email_ID"
            pos: 180,80

<option>:
    name: 'option_screen'
    FloatLayout:
        Button:
            text:"Add Stock"
            size_hint: .1 ,.1
            pos: 150,150
            on_press : root.btn_addstock()

        Button:
            text:"Check Stoploss"
            size_hint: .1 ,.1
            pos: 250,120
            on_press : root.btn_stoplosslist()

<stockinput>:
    name: 'stockinput_screen'


    stock_name: stock_name
    stock_symbol: stock_symbol
    purchase_price: purchase_price
    stop_loss: stop_loss

    GridLayout:
        cols:1
        size: root.width - 200, root.height -200
        pos: 100, 100

        GridLayout:
            cols:2

            Label:
                text: "Stock Name: "

            TextInput:
                id: stock_name
                multiline:False

            Label:
                text: "Stock Symbol: "

            TextInput:
                id: stock_symbol
                multiline:False

            Label:
                text: "Purchase Price: "

            TextInput:
                id: purchase_price
                multiline:False

            Label:
                text: "Stop Loss(%): "

            TextInput:
                id: stop_loss
                multiline:False

        Button:
            text:"Submit"

            size_hint: 1.5 ,.3
            pos: 350,180
            on_press: root.btn()
        Button:
            text:"Back"
            size_hint: 1.5 ,.3
            pos: 350,140
            on_press: root.manager.current = 'option_screen'

<ListApp>:
    name: 'Stoploss_ip'

    FloatLayout:
        Button:
            text: "Check"
            size_hint: .5 ,.3
            pos: 0,200
            on_press: root.build()

        Button:
            text:"Back"
            size_hint: .5 ,.3
            pos: 0,0
            on_press: app.root.current = 'option_screen'

带有新代码的新更新问题

在下面的代码中,我想将所选项目显示到 class MyTextInput 中的 text_box1 以及 class SelectableLabel 方法中显示文本框的代码 apply_selection test.py 文件

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

但我收到以下错误:

"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

您将 ListApp 视为 Screen,但它不是 Screen。我建议你重新设计 ListApp 使其成为 Screen.

我无法 运行 你的代码,但我会先更改 ListApp:

class ListApp(Screen):
    # def Wstop(self,event):
    #     ListApp().stop()

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

        end = datetime(2020, 12, 10)
        start = datetime(2020, 12, 10)
        btn = Button(text="Back",
                     font_size="20sp",
                     background_color=(1, 1, 1, 1),
                     color=(1, 1, 1, 1),
                     size=(12, 12),
                     size_hint=(.1, .05),
                     pos=(600, 500), on_press=self.Wstop)
        screen = self
        .
        .
        .

基本上将class定义中的MDApp更改为Screen,将build()方法更改为__init__(),添加一个super()调用,并将 screen = Screen() 更改为 screen = self。并且不再需要 return screen

我不认为这会在第一次尝试时奏效,但它应该能让你入门。