简体   繁体   English

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

[英]kivy - textinput from kv to py - import data

I'm trying to get the data from a simple textinput that is in my kv file and use the information in my python file. 我正在尝试从kv文件中的简单textinput获取数据,并使用python文件中的信息。 but i keep getting the same error. 但我一直收到相同的错误。 this error happens when i press a button and the function try to run looks like its trying to get the information from the text_location widget thank you in advance. 当我按下一个按钮并且函数尝试运行时会发生此错误,就像它试图从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()

the kivy file called "historical.kv" i reduce it because is kind of long 名为“ 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

the error i receive is 我收到的错误是

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__'

thank you in advance best, 最好先谢谢你,

First, you need to remove both of the id:input1 lines and the id:input2 line. 首先,您需要删除id:input1行和id:input2行。 Then change your initialize_request method to: 然后将您的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}')

Note that self.ids.text_location is a reference to the TextInput widget, so you have to add .text to get its text. 请注意, self.ids.text_location是对TextInput小部件的引用,因此您必须添加.text才能获取其文本。 Same for the other TextInput widgets. 与其他TextInput小部件相同。

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

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