from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.recycleview.views import RecycleDataViewBehavior from kivy.uix.label import Label from kivy.properties import BooleanProperty, ObjectProperty from kivy.uix.recycleboxlayout import RecycleBoxLayout from kivy.uix.behaviors import FocusBehavior from kivy.uix.recycleview.layout import LayoutSelectionBehavior from kivy.network.urlrequest import UrlRequest from kivy.lang import Builder
import json
classSelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout): ''' Adds selection and focus behaviour to the view. '''
classSelectableLabel(RecycleDataViewBehavior, Label): ''' Add selection support to the Label ''' index = None selected = BooleanProperty(False) selectable = BooleanProperty(True)
defrefresh_view_attrs(self, rv, index, data): ''' Catch and handle the view changes ''' self.index = index returnsuper(SelectableLabel, self).refresh_view_attrs( rv, index, data)
defon_touch_down(self, touch): ''' Add selection on touch down ''' ifsuper(SelectableLabel, self).on_touch_down(touch): returnTrue if self.collide_point(*touch.pos) and self.selectable: return self.parent.select_with_touch(self.index, touch)
defapply_selection(self, rv, index, is_selected): ''' Respond to the selection of items in the view. ''' self.selected = is_selected
defsearch_location(self): search_template = "https://samples.openweathermap.org/data/2.5/find?q={}&appid=b6907d289e10d714a6e88b30761fae22" # search_template = "https://api.openweathermap.org/data/2.5/find?q={}&typle=like&appid=xyz" # Replace 'xyz' with your API Key (APPID) search_url = search_template.format(self.search_input.text) request = UrlRequest(search_url, self.found_location)
deffound_location(self, request, data): data = json.loads(data.decode()) ifnotisinstance(data, dict) else data cities = ["{} ({})".format(d['name'], d['sys']['country']) for d in data['list']] self.search_results.data = [{'text': str(x)} for x in cities] print(f"self.search_results.data={self.search_results.data}")
from kivy.app import App ##kivy.require("1.9.1") from kivy.uix.boxlayout import BoxLayout from kivy.properties import ObjectProperty from kivy.network.urlrequest import UrlRequest