나는 Kivy를 배우고 있으며이 코드 조각이 위젯을 캐시하는 것이라면 목적을 알고 있지만 무슨 일이 일어나고 있는지 이해하는 데 어려움이 있습니다.
class WeatherRoot(BoxLayout):
current_weather = ObjectProperty()
def show_current_weather(self, location=None):
self.clear_widgets()
print(WeatherRoot.current_weather)
print(self.current_weather)
if location is None and self.current_weather is None:
location = 'New York (US)'
if location is not None:
self.current_weather = Factory.CurrentWeather()
self.current_weather.location = location
self.add_widget(self.current_weather)
문제는 current_weather
이것이 클래스 변수라는 것을 아는 한으로 정의되어 ObjectProperty
있으며이 변수를 재정의하는 인스턴스 변수가 없기 때문에 (내 생각에는 그렇게 생각합니다) self.current_weather
참조 할 때 클래스를 참조하고 있습니다 변수, 그래서 나는 그것이 self.current_weather
와 동일 하다고 생각 WeatherRoot.current_weather
하지만, 그 변수를 인쇄했을 때 둘 다 ObjectProperty
인스턴스 가 될 것으로 예상했기 때문에 그렇지 않습니다 .
<ObjectProperty name=current_weather>
None
내 요점은이 변수가 None
클래스 변수이기 때문에 결코 될 수 없다는 것입니다. 그래서 항상 ObjectProperty
이지만 가능해 보이며 그 None
이유를 이해할 수 없습니다.
다음은이 애플리케이션의 GUI입니다.
이것은 내 Kivy 파일입니다.
WeatherRoot:
<WeatherRoot>:
AddLocationForm
<LocationButton>:
on_press: app.root.show_current_weather(self.text)
<AddLocationForm>:
orientation: 'vertical'
search_input: search_box
search_results: search_results_list
BoxLayout:
height: '40dp'
size_hint_y: None
TextInput:
id: search_box
size_hint_x: 50
focus: True
multiline: False
on_text_validate: root.search_location()
Button:
text: 'Search'
size_hint_x: 25
on_press: root.search_location()
Button:
text: 'Current Search'
size_hint_x: 25
ListView:
id: search_results_list
adapter:
ListAdapter(data=[], cls=main.LocationButton)
Button:
height: '40dp'
size_hint_y: None
text: 'Cancel'
on_press: app.root.show_current_weather(None)
따라서 취소 버튼을 눌렀을 때 이전에 검색된 위치가없는 경우에서 볼 수 있듯이 기본값이 하드 코딩됩니다 'New York (US)
. 이전에 위치를 검색하고 취소 버튼을 누르면이 위치가 표시됩니다.
누군가이 변수로 무슨 일이 일어나고 있는지 설명해 주 current_weather
시겠습니까? 이 클래스 변수가 필요하지 않다고 생각했지만 삭제했을 때 앱이 충돌했습니다.
필요한 경우 이것은 내가 가진 전체 코드입니다.
import json
from kivy.app import App
from kivy.network.urlrequest import UrlRequest
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.listview import ListItemButton
from kivy.factory import Factory
class WeatherRoot(BoxLayout):
current_weather = ObjectProperty()
def show_current_weather(self, location=None):
self.clear_widgets()
print(WeatherRoot.current_weather)
print(self.current_weather)
if location is None and self.current_weather is None:
location = 'New York (US)'
if location is not None:
self.current_weather = Factory.CurrentWeather()
self.current_weather.location = location
self.add_widget(self.current_weather)
def show_add_location_form(self):
self.clear_widgets()
self.add_widget(AddLocationForm())
class LocationButton(ListItemButton):
pass
class AddLocationForm(BoxLayout):
search_input = ObjectProperty()
search_results = ObjectProperty()
def search_location(self):
search_template = 'http://api.openweathermap.org/' \
'data/2.5/find?q={}&type=like&APPID=' \
'090428d02304be901047796d430986e3'
search_url = search_template.format(self.search_input.text)
print(search_url)
request = UrlRequest(search_url, self.found_location)
def found_location(self, request, data):
data = json.loads(data.decode()) if not isinstance(data, dict) else data
cities = ['{} ({})'.format(d['name'], d['sys']['country'])
for d in data['list']]
# self.search_results.item_strings = cities
self.search_results.adapter.data.clear()
self.search_results.adapter.data.extend(cities)
self.search_results._trigger_reset_populate()
class WeatherApp(App):
pass
WeatherApp().run()
속성은 설명자 입니다. 클래스 수준에서 정의되었지만 인스턴스 수준의 동작이 있으며 첫 번째 근사값은 인스턴스 변수처럼 동작합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다