main.py的一部分:
class search(Screen):
def __init__(self, **kwargs):
Builder.load_file('search.kv')
super(search, self).__init__(**kwargs)
def searchNow(self):
location = self.ids.location_search.text
result = self.apiCall(location)
tmpList = list()
for entry in (result["data"]):
tmpList.append(entry["result_object"]["location_string"])
tmpList = list(dict.fromkeys(tmpList))
count = len(tmpList)
fill = 10 - count
for i in range(fill):
tmpList.append("")
self.ids.sResult1.text = tmpList[0]
self.ids.sResult2.text = tmpList[1]
self.ids.sResult3.text = tmpList[2]
self.ids.sResult4.text = tmpList[3]
self.ids.sResult5.text = tmpList[4]
self.ids.sResult6.text = tmpList[5]
self.ids.sResult7.text = tmpList[6]
self.ids.sResult8.text = tmpList[7]
self.ids.sResult9.text = tmpList[8]
self.ids.sResult10.text = tmpList[9]
if (fill == 10):
self.ids.sResult1.text = "No Search Results"
def sR1(self):
if self.ids.sResult1.text == "":
return
self.dispSearchResult(self.ids.sResult1.text)
def sR2(self):
if self.ids.sResult1.text == "":
return
self.dispSearchResult(self.ids.sResult2.text)
def sR3(self):
if self.ids.sResult1.text == "":
return
self.dispSearchResult(self.ids.sResult3.text)
def sR4(self):
if self.ids.sResult1.text == "":
return
self.dispSearchResult(self.ids.sResult4.text)
def sR5(self):
if self.ids.sResult1.text == "":
return
self.dispSearchResult(self.ids.sResult5.text)
def sR6(self):
if self.ids.sResult1.text == "":
return
self.dispSearchResult(self.ids.sResult6.text)
def sR7(self):
if self.ids.sResult1.text == "":
return
self.dispSearchResult(self.ids.sResult7.text)
def sR8(self):
if self.ids.sResult1.text == "":
return
self.dispSearchResult(self.ids.sResult8.text)
def sR9(self):
if self.ids.sResult1.text == "":
return
self.dispSearchResult(self.ids.sResult9.text)
def sR10(self):
if self.ids.sResult1.text == "":
return
self.dispSearchResult(self.ids.sResult10.text)
def dispSearchResult(self, locString):
result = self.apiCall(locString)
SRname = (result["data"][0]["result_object"]["location_string"])
SRimg = (result["data"][0]["result_object"]["photo"]["images"]["large"]["url"])
SRdescription = (result["data"][0]["result_object"]["geo_description"])
resultParams = list()
resultParams[0] = SRname
resultParams[1] = SRimg
resultParams[2] = SRdescription
SCREEN_MANAGER.current = 'sresult'
def apiCall(self, location):
url = "https://tripadvisor1.p.rapidapi.com/locations/search"
querystring = {"query": location, "lang": "en_US", "units": "mi"}
headers = {
REDACTED
}
response = requests.get(url, headers=headers, params=querystring)
result = response.json()
print (response.text)
return result
class sresult(Screen):
def __init__(self, **kwargs):
Builder.load_file('sresult.kv')
super(sresult, self).__init__(**kwargs)
Builder.load_file('main.kv')
SCREEN_MANAGER.add_widget(MainScreen(name='main'))
SCREEN_MANAGER.add_widget(one(name='one'))
SCREEN_MANAGER.add_widget(why(name='why'))
SCREEN_MANAGER.add_widget(two(name='two'))
SCREEN_MANAGER.add_widget(search(name='search'))
SCREEN_MANAGER.add_widget(sresult(name='sresult'))
if __name__ == "__main__":
TripMe().run()
sresult.kv:
<sresult>:
name: 'sresult'
FloatLayout:
size_hint: None, None
Image:
source: "TRIPME.png"
x: (root.width * .5)-100
y: root.height * .8
size_hint_x: 2
allow_stretch: True
AsyncImage:
source: SRimg
我在传递图像链接(以及其他变量SRname
和SRdescription
)时遇到麻烦。我试过做source: app.SRimg
和source: Screen.SRimg
。我想避免创建全局变量,但是如果您知道如何使它起作用,那么任何帮助都可以。
如果将id
添加到AsyncImage
中的sresult.kv
:
<sresult>:
name: 'sresult'
FloatLayout:
size_hint: None, None
Image:
source: "TRIPME.png"
x: (root.width * .5)-100
y: root.height * .8
size_hint_x: 2
allow_stretch: True
AsyncImage:
id: asyncimage
source: SRimg
然后在dispSearchResult()
方法中,可以使用id
和SCREEN_MANAGER
设置源:
def dispSearchResult(self, locString):
result = self.apiCall(locString)
SRname = (result["data"][0]["result_object"]["location_string"])
SRimg = (result["data"][0]["result_object"]["photo"]["images"]["large"]["url"])
SRdescription = (result["data"][0]["result_object"]["geo_description"])
resultParams = list()
resultParams[0] = SRname
resultParams[1] = SRimg
resultParams[2] = SRdescription
# pass the URL of the image to the AsyncImage instance
SCREEN_MANAGER.get_screen('sresult').ids.asynchimage.source = SRimg
SCREEN_MANAGER.current = 'sresult'
我尚未测试此代码,但我相信它会起作用。