C0mpl3x
C0mpl3x

Reputation: 532

Python gui format json

I'm trying to show a json in my gui but right now it looks like this:

enter image description here

I want it to be properly formated. I've tried using this :

json_text = StringVar()
json_formatted = json.dumps(data, indent=4, sort_keys=True)
json_text.set(json_formatted)

It worked when I printed it but not in my gui I've also tried using:

test_json_text = Label(o_canvas_l, anchor='w', textvariable=json_text)
test_json_text.grid(column=0, row=0, sticky=W)

Upvotes: 0

Views: 278

Answers (1)

kol
kol

Reputation: 28708

For multiline text the Message widget is better than Label. Set anchor="w" to make the text left-aligned. Unfortunately, it wraps long lines. The Text widget also works, it's left-aligned by default, and its text wrapping can be turned off. I would use something like:

text = tk.Text(frame, wrap=tk.NONE)
text.insert(tk.END, "before\nhello, world lorem ipsum\nafter")
text.configure(state="disabled")
text.pack()

Upvotes: 1

Related Questions