# How do i save what i write in here to a json file?

**URL:** https://discuss.python.org/t/how-do-i-save-what-i-write-in-here-to-a-json-file/79740
**Category:** Python Help
**Tags:** help, typing
**Created:** [February 6, 2025, 8:22pm UTC](https://discuss.python.org/t/how-do-i-save-what-i-write-in-here-to-a-json-file/79740 "2025-02-06T20:22:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![skateindys](https://avatars.discourse-cdn.com/v4/letter/s/ed8c4c/32.png) [@skateindys](https://discuss.python.org/u/skateindys)
#### Post date: [February 6, 2025, 8:22pm UTC](https://discuss.python.org/t/how-do-i-save-what-i-write-in-here-to-a-json-file/79740/1 "2025-02-06T20:22:10Z")

</div>

```python
import tkinter as tk

root = tk.Tk()
root.geometry("1000x750")
root.resizable(False, False)
root.title("My Notes")

my_text = tk.Text(root, font=('Arial', 18), width=100, height=75)
my_text.pack()

root.mainloop()

```

---

<div class="post-metadata">

### Author: ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)
#### Post date: [February 6, 2025, 8:59pm UTC](https://discuss.python.org/t/how-do-i-save-what-i-write-in-here-to-a-json-file/79740/2 "2025-02-06T20:59:48Z")

</div>

You can get the text from the widget with the `my_text.get` method.

If you want all of the text, give the start index as `'1.0'` (line 1, column 0) and the end index as `'end'`.

Note that after `root.mainloop()` returns, the widget won’t exist any longer, so here I’m getting it when the window closes:

```python
import tkinter as tk

root = tk.Tk()
root.geometry("1000x750")
root.resizable(False, False)
root.title("My Notes")

my_text = tk.Text(root, font=('Arial', 18), width=100, height=75)
my_text.pack()

def on_close():
    print(my_text.get('1.0', 'end'))
    root.destroy()

root.protocol('WM_DELETE_WINDOW', on_close)
root.mainloop()

```

---

<div class="post-metadata">

### Author: ![AlSweigart](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/alsweigart/32/12237_2.png) [@AlSweigart](https://discuss.python.org/u/AlSweigart)
#### Post date: [February 6, 2025, 9:49pm UTC](https://discuss.python.org/t/how-do-i-save-what-i-write-in-here-to-a-json-file/79740/3 "2025-02-06T21:49:44Z")

</div>

You can use the `json` module’s `dump()` function to write out the text to a JSON file. I had ChatGPT write out the code (though it took several iterations to get right and I needed to add a Save button and a confirmation message box. Also it can only save to _notes.json_ but you can edit this if needed.):

```python
import tkinter as tk
import json
from tkinter import messagebox

def save_to_json():
    text_content = my_text.get("1.0", tk.END).strip() # Get text from the widget
    data = {"notes": text_content} # Store in a dictionary

    with open("notes.json", "w", encoding="utf-8") as file:
        json.dump(data, file, indent=4) # Save as JSON

    # Show a message box confirming save
    messagebox.showinfo("Save Successful", "File saved as notes.json")

# Create main window
root = tk.Tk()
root.geometry("1000x750")
root.resizable(False, False)
root.title("My Notes")

# Create a frame to organize widgets
frame = tk.Frame(root)
frame.pack(fill="both", expand=True)

# Create text area
my_text = tk.Text(frame, font=('Arial', 18))
my_text.pack(fill="both", expand=True, side="top")

# Save button (placed at the bottom)
save_button = tk.Button(root, text="Save", font=("Arial", 14), command=save_to_json)
save_button.pack(side="bottom", pady=10) # Ensure it appears at the bottom

root.mainloop()

```
