繁体   English   中英

如何将json文件中的值附加到python中的tkinter组合框?

[英]How to append values from json file to tkinter combobox in python?

我正在尝试制作配置文件并将 json 数据保存到我已经成功完成的 txt 文件中,现在我希望能够搜索我的 json 文件并将每个配置文件名称添加到组合框。 这是生成的json数据的示例。

{
"profile": [
    {
      "profile_name": 'name',
      "address": 'address',
      "city": 'yorktown',
    }
  ]
}

我试图做的是用 for 循环访问数据,说

with open('profiles.txt', 'r') as file:
     profiles = json.load(file)
     for profile in profiles['profile']:
         profiles_select['values'] = profile['profile_name']

此解决方案有效但仅适用于一个配置文件,一旦我添加第二个配置文件,我就会收到此错误

Extra data: line 19 column 1 (char 430)

具有多个配置文件的 json 文件将如下所示

{
"profile": [
    {
      "profile_name": 'name',
      "address": 'address',
      "city": 'yorktown',
    }
  ]
}
{
"profile": [
    {
      "profile_name": 'name',
      "address": 'address',
      "city": 'yorktown',
    }
  ]
}

所以我认为这是因为它不知道何时停止或开始检查每个值,所以我尝试按文件中的行进行检查。 我试过这个代码

    with open('profiles.txt', 'r') as file:
        lines = file.readlines()
        for lines in file:
            profile_data = json.load(file)
            for profile in profile_data['profile']:
                profiles_select['values'] = profile['profile_name']

这不会产生错误,但不会在配置文件组合框中返回任何文本。 如果有人在这里有任何见解,那将非常有帮助。

这是我如何编写我的 json 数据

    def save_profile():
        try:
            with open('profiles.txt', 'r', encoding='utf-8') as infile:
                load_data_profile = json.load(infile)
        except:
            load_data_profile = {'profile': []}  # default when file can't be read
        load_data_profile['profile'].append({
            'profile_name': profile_name_entry.get(),
            'first_name': first_name_entry.get(),
            'last_name': last_name_entry.get(),
            'address': house_address_entry.get(),
            'address2': house_address2_entry.get(),
            'city': city_entry.get(),
            'country': country_entry.get(),
            'state': state_entry.get(),
            'zip': zip_entry.get(),
            'card_type': card_type_entry.get(),
            'card_number': card_number_entry.get(),
            'exp_month': card_exp_month_date_entry.get(),
            'exp_year': card_exp_year_date_entry.get(),
            'card_cvv': card_cvv_entry.get(),
            'phone': phone_entry.get(),
            'email': email_entry.get()
        })
        with open('profiles.txt', 'w', encoding='utf-8') as outfile:
            json.dump(load_data_profile, outfile, indent=4)

您的 JSON 是错误的,所以假设对于多个配置文件,它实际上看起来像这样:

[{
"profile": [
    {
      "profile_name": 'name',
      "address": 'address',
      "city": 'yorktown',
    }
  ]
},
{
"profile": [
    {
      "profile_name": 'name',
      "address": 'address',
      "city": 'yorktown',
    }
  ]
}]

现在在这种情况下,您应该能够像这样解析它:

with open('profiles.txt', 'r') as file:
     profiles = json.load(file)
     for item in profiles:
         profile = item["profile"]
         profiles_select['values'] = profile['profile_name']

但请注意,您实际上根本不需要profile密钥。 你可以只拥有一个 JSON 对象数组:

    [{
      "profile_name": 'name',
      "address": 'address',
      "city": 'yorktown',
    },
    {
      "profile_name": 'name',
      "address": 'address',
      "city": 'yorktown',
    }]

然后有:

with open('profiles.txt', 'r') as file:
     profiles = json.load(file)
     for profile in profiles:
         profiles_select['values'] = profile['profile_name']

暂无
暂无

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

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