如何将捕获的输入从 tkinter 条目小部件写入 json 文件 [英] How to write captured input from a tkinter entry widget to a json file

查看:31
本文介绍了如何将捕获的输入从 tkinter 条目小部件写入 json 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您的时间.我简化了这个问题的结构,使其尽可能简单地解决.我正在与 GUI 开发人员 tkinter 一起使用 python.我想从 tkinter 中的条目小部件中捕获文本输入并将其打印/写入 json 文件.到目前为止,我所拥有的是这个示例,它从条目小部件中捕获文本并在 shell 终端中打印文本.

Thank you for your time. I reduced the structure of this problem to make it as simple to solve as possible. I am working in python with the GUI developer, tkinter. I want to capture the text input from an entry widget in tkinter and print/write it in a json file. What I have so far is this example, which capture the text from the entry widgets and prints the text in the shell terminal.

from tkinter import *

def show_entry_fields():
   print("First Name: %s\nLast Name: %s" % (e1.get(), e2.get()))
   e1.delete(0,END)
   e2.delete(0,END)

master = Tk()
Label(master, text="First Name").grid(row=0)
Label(master, text="Last Name").grid(row=1)

e1 = Entry(master)
e2 = Entry(master)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

Button(master, text='Show', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)

mainloop( )

这段代码完全符合我的要求,除了文本打印在 shell 终端而不是单独的 json 文件中. 要在 json 文件中打印文本,我尝试了替代方法函数方法如:

This code does exactly what I want to do, except that the text is printed in the shell terminal instead of a separate json file. To print the text in a json file, I have tried alternative function methods like:

def show_entry_fields():
   f = open('name.json', 'a', encoding = 'utf8')
   print("First Name: %s\nLast Name: %s" % (e1.get(), e2.get()))
   e1.delete(0,END)
   e2.delete(0,END)

在这里,我希望该函数能够打开 json 文件name.json"并以与第一个示例在 shell 终端中打印文本相同的方式打印该文件中的文本.这没有成功.

Here, I hoped that the function would open the json file 'name.json' and print the text in that file in the same way that the first example printed the text in the shell terminal. This was not successful.

还有:

def show_entry_fields():
   f = open('name.json', 'a', encoding = 'utf8')
   f.write("First Name: %s\nLast Name: %s" % (e1.get(), e2.get()))
   e1.delete(0,END)
   e2.delete(0,END)

在这里,我希望能实现同样的目标,但还是没有成功.

Here, I hoped that the same goal would be accomplished, but again it was unsuccessful.

最终,目标是打印输入到name.json"文件中的条目小部件中的文本.虽然我已经能够从终端外壳中的条目小部件打印文本(参见第一个示例),但我无法找到打开 json 文件并打印该文件中文本的函数.谢谢!

Ultimately, the goal is to print the text entered into the entry widget in the 'name.json' file. While I have been able to print the text from the entry widget in the terminal shell (see the first example), I have not been able to find a function to open a json file and print the text in that file. Thanks!

这与任何其他问题不同,因为该问题专门询问如何将捕获的文本导出到 json 文件.前面的问题专门涉及捕获文本.

This is different than any other question because this question asks specifically how to export captured text to a json file. Previous questions deal specifically with capturing the text.

推荐答案

如果要写json,第一步就是创建一个可以转json的数据结构.然后,您可以使用 json.dump 函数将数据写入文件.

If you want to write json, the first step is to create a data structure that can be converted to json. Then, you can use the json.dump function to write the data to a file.

示例:

import json
...
data = {
    "first_name": e1.get(),
    "last_name": e2.get()
}
with open('name.json', 'w') as f:
    json.dump(data, f, indent=4)

当您执行该代码并输入Foo"作为名字和Bar"作为姓氏时,文件将以此作为其内容:

When you execute that code, and enter "Foo" for the first name and "Bar" for the last name, the file will have this as its contents:

{
    "first_name": "Foo", 
    "last_name": "Bar"
}

这篇关于如何将捕获的输入从 tkinter 条目小部件写入 json 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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