我正在使用 PySimpleGUI,我想為下拉串列創建動態選項。
我的代碼:
#in file gui.py
import PySimpleGUI as psg
layout = [[psg.Text('Choose category:', size=(20, 1), font='Lucida', justification='left')],
[psg.Combo([prep_data.prepare_dropdown_categories()],
default_value='All crimes', key='all')],
[psg.Button('SAVE', font=('Lucida', 12)), psg.Button('CANCEL', font=('Lucida', 12))]]
#in file prep_data.py
def prepare_dropdown_categories():
categories = []
fetched_categories = fetch_categories() #this fetches categories from an api, returns json.loads(data)
for category in fetched_categories:
categories.append(category['name'])
return categories
我想要的具體資料(api):https : //data.police.uk/api/crime-categories
我的結果是一個下拉串列,其中一個選項存盤所有“名稱”字串:
{name0}{name1}{name2}name3{name4}
是的,并非所有人都圍繞著花括號......我希望有人知道如何正確地做到這一點。
感謝您的幫助。
uj5u.com熱心網友回復:
名字沒問題 - 見下文。確保您獲得相同的串列。
import requests
r = requests.get('https://data.police.uk/api/crime-categories')
if r.status_code == 200:
print([x['name'] for x in r.json()])
輸出
['All crime', 'Anti-social behaviour', 'Bicycle theft', 'Burglary', 'Criminal damage and arson', 'Drugs', 'Other theft', 'Possession of weapons', 'Public order', 'Robbery', 'Shoplifting', 'Theft from the person', 'Vehicle crime', 'Violence and sexual offences', 'Other crime']
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/333852.html
下一篇:AJAX回應不會在控制臺中列印
