0

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)
4
  • 2
    The text is centered. You have to set left-aligned text for the GUI element that displays it. Commented Jul 6, 2021 at 14:06
  • @kol I've tried (I edited the question) but for some reason it didn't wok Commented Jul 6, 2021 at 14:24
  • 1
    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, 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() Commented Jul 6, 2021 at 15:01
  • 1
    @kol Thanks it worked, if you put it as answer I can mark it as the correct sollution Commented Jul 7, 2021 at 8:59

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.