-1

how do i manually parse output of .dump() from a text widget that has italic and bold text in it to a different text widget to load it along with its text format.

this is the code if it helps, the text should be saved along with text format but when the file is open the text format is gone:

from tkinter import *
from tkinter import filedialog
root = Tk()

textpad=Text(root, font='Consolas 11')
textpad.pack()

def save():
    content=textpad.get(1.0,'end')
    filename=filedialog.asksaveasfilename(title='open file',filetypes=(("text","*.txt"),("html",".html"),("all types","*.*")))
    openit=open(filename,'w')
    openit.write(content)
    openit.close()

def opens():
    textpad.delete(1.0,'end')
    filename=filedialog.askopenfilename(title='open file',filetypes=(("text","*.txt"),("html",".html"),("all types","*.*")))
    openit=open(filename,'r')
    content=openit.read()
    textpad.insert('end',content.strip())
    openit.close()

def boldtext():
    textpad.selection_get()
    textpad.tag_add('bold','sel.first','sel.last')
    textpad.tag_config('bold',font='Consolas 11 bold')

savebtn=Button(text='save',command=save)
savebtn.pack()
openbtn=Button(text='open',command=opens)
openbtn.pack()
bold=Button(text='bold',command=boldtext)
bold.pack()
root.mainloop()
  • `.get()` will not include those formatting information. Also what is `.dump()`? – acw1668 Oct 25 '22 at 11:01
  • based on what i experience so far, when i used dump method to a text widget it extracts the information about that text widget including the tags information – ILoveGrapes Oct 25 '22 at 12:37
  • @acw1668: `dump` is a method that gets the formatting information along with the text. – Bryan Oakley Oct 25 '22 at 15:41
  • The code you posted doesn't include any call to `dump` or any attempt to parse it's output. It's not clear what the problem is that you need help with since the formation of the output of `dump` is documented. – Bryan Oakley Oct 25 '22 at 15:42
  • I suggest you start with some research on how to use the `dump` command. Here's an answer with an example that converts the data to html: https://stackoverflow.com/a/52915925/7432. Instead of emitting html tags, you can call the text widget `insert` method to reproduce the information in another text widget. – Bryan Oakley Oct 25 '22 at 15:43

1 Answers1

0

The Text widget dump method returns a list of tuples. Each tuple will be of the form (key, value, index). key will be one of the following: text, mark, tagon, tagoff, image, or window. value will be dependent on the key. index is the location in the text. For example, with tagon and tagoff the value will be the name of the tag. For text it's a string of text characters.

Consider a text widget with the tags "bold" for bold and "italic" for italic. It might look something like this:

screenshot of text widget with bold and italicized text

When you call the dump method you will get something like the following (line breaks added for clarity):

[('text', 'Here is a sentence with ', '1.0'),
 ('tagon', 'bold', '1.24'),
 ('text', 'bolded', '1.24'),
 ('tagoff', 'bold', '1.30'),
 ('text', ' and ', '1.30'),
 ('tagon', 'italic', '1.35'),
 ('text', 'italicized', '1.35'),
 ('tagoff', 'italic', '1.45'),
 ('text', ' text.', '1.45')]

A conversion function needs to loop over that data and process each key. For example, for every tagon you can add the tag to a list, and for every tagoff you can remove it from the list. For the text tag you just insert the text with the current list of tags.

The solution might look something like this:

def undump(dump_data, target_widget, index="1.0"):
    tags = []
    target.mark_set("paste_index", index)
    for (key, value, index) in dump_data:
        if key == "tagon":
            tags.append(value)
        elif key == "tagoff":
            tags.remove(value)
        elif key == "mark":
            target.mark_set(value, index)
        elif key == "text":
            target.insert("paste_index", value, tags)

Given a original text widget named source and a second text widget named target, you would call it like this:

undump(source.dump("1.0", "end-1c"), target)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685