0

I've been trying to work around this problem whole previous day. And almost succeeded. But still, I didn't get a clear answer about this - why the next code doesn't work?

import sqlite3
from tkinter import *
from tkinter import ttk
root = Tk()
text = Text(root)
text.pack()
con = sqlite3.connect(':memory:')
c = con.cursor()
c.execute("CREATE TABLE t1 (name)")
c.execute("INSERT INTO t1 VALUES (?)", ('TEST',))
con.commit()

c.execute("SELECT * FROM t1")
text_ = c.fetchall()
text.insert(INSERT, text_)
text.tag_add('one', '1.0', '1.3')
text.tag_config('one', background='blue')

text_get = text.get('1.0', 'end')


c.execute("UPDATE t1 SET name=?", (text_get,))
con.commit()


text.insert(INSERT, '\n')

c.execute("SELECT * FROM t1")
text_ = c.fetchall()
text.insert(INSERT, text_)


root.mainloop()

The result is next: TEXT before saving into db - has a background colour TEXT after updating into db and retrieving it back - doesn't. Is there any way to make it work?

1 Answer 1

1

The Text.get() method returns only the plain text, but not information about any tags.

You have to use other methods to get information about the tags:

>>> text.tag_names()
('sel', 'one')
>>> text.tag_cget('one', 'background')
'blue'
>>> text.tag_ranges('one')
(<textindex object: '1.0'>, <textindex object: '1.3'>)

That information needs to be stored in some other column(s). Alternatively, insert formatting codes into the text saved in the DB.

Sign up to request clarification or add additional context in comments.

1 Comment

@CL.Thank you. It works. Unfortunately it means a lot of code to re write... But for the future I will use it.

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.