kivy-从kv到py的textinput-导入数据

时间:2019-01-02 17:25:03

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

我正在尝试从kv文件中的简单textinput获取数据,并使用python文件中的信息。 但我一直收到相同的错误。 当我按下一个按钮并且函数尝试运行时会发生此错误,就像它试图从text_location小部件获取信息一样 预先谢谢你。

class ScatterTextWidget(BoxLayout):


    def initialize_request(self):
         ''' Initial analysis and control flow of the class '''

         location = self.ids.input_location
         date = S.ids.input_date
         time_raw = ScatterTextWidget.ids.input_time
         print(f'{location} - {date} - {time_raw}')

class HistoricalApp(App):
     def build(self):
         return ScatterTextWidget()

if __name__ == "__main__":

    HistoricalApp().run()

名为“ historical.kv”的kivy文件 我减少它,因为有点长

<ScatterTextWidget>:
    BoxLayout:
        canvas.before:
            Color:
                rgb: utils.get_color_from_hex('#01183f')
            Rectangle:
                pos: self.pos
                size: self.size
        size_hint_y: None
        height: 40
        valign: 'middle'

        TextInput:
            id:text_location
            width: 200
            id: input2
            font_size: 15
            size_hint_y: None
            height:40
        TextInput:
            id:input_date
            width: 200
            id: input1
            font_size: 15
            size_hint_y: None
            height: 40
        TextInput:
            id:input_time
            width: 200
            id: input1
            font_size: 15
            size_hint_y: None
            height: 40

我收到的错误是

File "kivy\properties.pyx", line 838, in 
 kivy.properties.ObservableDict.__getattr__
 KeyError: 'text_location'

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "C:/Users/IBKCo/Desktop/Git Programs/TimeStationHistoricalRecords/MainGui.py", line 77, in <module>
     HistoricalApp().run()
   File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\app.py", line 826, in run
     runTouchApp()
   File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\base.py", line 502, in runTouchApp
     EventLoop.window.mainloop()
   File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\core\window\window_sdl2.py", line 727, in mainloop
     self._mainloop()
   File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\core\window\window_sdl2.py", line 460, in _mainloop
     EventLoop.idle()
   File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\base.py", line 340, in idle
     self.dispatch_input()
   File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\base.py", line 325, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\base.py", line 291, in post_dispatch_input
     wid.dispatch('on_touch_up', me)
   File "kivy\_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\uix\behaviors\button.py", line 179, in on_touch_up
     self.dispatch('on_release')
   File "kivy\_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
   File "kivy\_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
   File "kivy\_event.pyx", line 1098, in kivy._event.EventObservers._dispatch
   File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\pdEnv\lib\site-packages\kivy\lang\builder.py", line 64, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "C:\Users\IBKCo\Desktop\Git Programs\TimeStationHistoricalRecords\historical.kv", line 127, in <module>
     on_release: root.initialize_request()
   File "C:/Users/IBKCo/Desktop/Git Programs/TimeStationHistoricalRecords/MainGui.py", line 31, in initialize_request
     location = self.ids.text_location
   File "kivy\properties.pyx", line 841, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

先谢谢你 最好,

1 个答案:

答案 0 :(得分:2)

首先,您需要同时删除id:input1行和id:input2行。然后将您的initialize_request方法更改为:

def initialize_request(self):
     ''' Initial analysis and control flow of the class '''

     location = self.ids.text_location.text
     date = self.ids.input_date.text
     time_raw = self.ids.input_time.text
     print(f'{location} - {date} - {time_raw}')

请注意,self.ids.text_location是对TextInput小部件的引用,因此您必须添加.text才能获取其文本。与其他TextInput小部件相同。