在python中为gui从对象创建字符串列表



我正在使用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

我的结果是一个下拉菜单,其中一个选项存储所有'name'字符串:

{name0}{name1}{name2}name3{name4}

,是的,并不是所有的都有花括号…我希望有人知道如何正确地做这件事。

谢谢你的帮助。

名称是OK的-见下文。确保你得到相同的列表。

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']

最新更新