简体   繁体   English

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

[英]Create list of strings for gui from object in python

I'm using PySimpleGUI and I'd like to create dynamic options for a dropdown.我正在使用 PySimpleGUI,我想为下拉列表创建动态选项。

my code:我的代码:

#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

specific data I want(api): https://data.police.uk/api/crime-categories我想要的具体数据(api): https : //data.police.uk/api/crime-categories

My outcome is a dropdown with one option storing all 'name' strings:我的结果是一个下拉列表,其中一个选项存储所有“名称”字符串:

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

and yes, not all of them have curly braces around them... I hope someone knows how to properly do this.是的,并非所有人都围绕着花括号......我希望有人知道如何正确地做到这一点。

Thank you for the help.感谢您的帮助。

The names are OK - see below.名字没问题 - 见下文。 Make sure you get the same list.确保您获得相同的列表。

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()]) 

output输出

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM