为什么我的 python 函数打印出 {}Main 而不仅仅是 Main? [英] Why is my python function printing out {}Main and not just Main?

查看:40
本文介绍了为什么我的 python 函数打印出 {}Main 而不仅仅是 Main?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个函数从 json 文件中获取我的数据并将其打印到 tkinter 组合框,出于某种原因,第一个单词总是在它前面有 {}.我的 json 数据没有它,为什么它会出现在我的输出中?

 with open('profiles.txt', 'r') 作为文件:配置文件 = json.load(file)对于配置文件中的配置文件:add_profile = profile['profile_name']profile_select['values'] = (profiles_select['values'], add_profile)

Values 也预设为不包含任何内容,因此值容器代码如下所示:profiles_select = ttk.Combobox(new_task_frame1, width=10, values=[])

这里是json示例数据:

<预><代码>[{"profile_name": "主要","first_name": "迈克尔 ",},{"profile_name": "测试","first_name": "迈克尔 ",}]

这是我希望在我的 tkinter 组合框中显示的内容:

主要测试

这是实际输出的内容

{}主要测试

解决方案

问题:为什么我的 Python 函数打印的是 '{}Main' 而不仅仅是 'Main'?

  • 初始化profiles_select['values']

    profiles_select = ttk.Combobox(new_task_frame1, width=10, values=[])

    这个结果,小部件内部,到:''tcl 使用:'{}'

<小时>
  • 添加选项

    profiles_select['values'] = (profiles_select['values'], add_profile)

    您采用内部值 '' 和一个新选项 'Main',结果为 ('', 'Main').立>

<小时>
  • tkinter 如何显示 Listbox 选项

    <块引用>

    您的两个选项:

    {}主要测试

    <块引用>

    超过两个选项:

    profiles_select['values'] = ('{{{} Main} Test} Test2', 'Test3'){{{} Main} 测试} Test2测试3

<小时><块引用>

结论,不要使用更新选项:
profiles_select['values'] = (profiles_select['values'], add_profile)

<小时><块引用>

解决方案:首先创建您的序列,然后将其整体分配一次.

 with open('profiles.txt', 'r') 作为文件:配置文件 = json.load(file)选项 = []对于配置文件中的配置文件:options.append(profile['profile_name'])profile_select['values'] = 选项

I am using a function to grab my data from a json file and print it to a tkinter combo box, for some reason the first word always has {} in front of it. My json data doesn't have it so why does it appear in my output?

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

Values is also preset to have nothing in it so the values container code looks like this: profiles_select = ttk.Combobox(new_task_frame1, width=10, values=[])

Here is the json sample data:

[
    {
        "profile_name": "Main",
        "first_name": "Michael ",
    },
    {
        "profile_name": "Test",
        "first_name": "Michael ",
    }
]

This is what i expect to show up in my tkinter combo box :

Main
Test

This is what actually outputs

{}Main
Test

解决方案

Question: Why is my python function printing out '{}Main' and not just 'Main'?

  • Initalizing profiles_select['values']

    profiles_select = ttk.Combobox(new_task_frame1, width=10, values=[])
    

    This results, widget internal, to: '', tcl uses: '{}'


  • Adding options

    profiles_select['values'] = (profiles_select['values'], add_profile)
    

    You take the internal value ''and a new option 'Main', which results in ('', 'Main').


  • How tkinter shows the Listbox options

    Your two options:

    {}Main  
    Test
    

    More than two options:

    profiles_select['values'] = ('{{{} Main} Test} Test2', 'Test3')
    
    {{{} Main} Test} Test2
    Test3
    


Conclusion, don't update options using:
profiles_select['values'] = (profiles_select['values'], add_profile)


Solution: Create your sequence first and assign it in whole once.

with open('profiles.txt', 'r') as file:
    profiles = json.load(file)

    options = []
    for profile in profiles:
        options.append(profile['profile_name'])

    profiles_select['values'] = options

这篇关于为什么我的 python 函数打印出 {}Main 而不仅仅是 Main?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆