简体   繁体   中英

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. 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.

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

<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. Then change your initialize_request method to:

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. Same for the other TextInput widgets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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