Tkinter - Text - bullets

T

Thomas Lehmann

My intention is to write a small custom widget displaying text where
the text can have a simple wiki syntax. The main interest is to
support heading, bold, italic, underline, itemization and enumeration.

How can I implement itemization using the Tkinter.Text widget?
(bullets)
 
E

eb303

My intention is to write a small custom widget displaying text where
the text can have a simple wiki syntax. The main interest is to
support heading, bold, italic, underline, itemization and enumeration.

How can I implement itemization using the Tkinter.Text widget?
(bullets)

Something like this maybe?
----
from Tkinter import *

root = Tk()
txt = Text(root, wrap='word')
txt.pack()

txt.tag_configure('text_body', font=('Times', 18), lmargin1=0,
lmargin2=0)
txt.tag_configure('bulleted_list', font=('Times', 18), lmargin1='10m',
lmargin2='15m', tabs=['15m'])

txt.insert(END, u"This is a normal paragraph. Let's make it a bit long
to see that it wraps as expected.\n", 'text_body')
txt.insert(END, u"\u00B7\tThis is the first item in the list.\n",
'bulleted_list')
txt.insert(END, u"\u00B7\tThis is the second item in the list. Let's
make this one quite long too to see how it wraps.\n", 'bulleted_list')

root.mainloop()
 
T

Thomas Lehmann

Something like this maybe?
----
from Tkinter import *

root = Tk()
txt = Text(root, wrap='word')
txt.pack()

txt.tag_configure('text_body', font=('Times', 18), lmargin1=0,
lmargin2=0)
txt.tag_configure('bulleted_list', font=('Times', 18), lmargin1='10m',
lmargin2='15m', tabs=['15m'])

txt.insert(END, u"This is a normal paragraph. Let's make it a bit long
to see that it wraps as expected.\n", 'text_body')
txt.insert(END, u"\u00B7\tThis is the first item in the list.\n",
'bulleted_list')
txt.insert(END, u"\u00B7\tThis is the second item in the list. Let's
make this one quite long too to see how it wraps.\n", 'bulleted_list')

Thank you very much!
However, the result is not that pretty as I have expected. The bullets
are really small. When separating bullet and text then I can increase
the font size for the bullet but then it does not fit to the text -
vertical alignment is wrong. Also it's pretty unhandy to adjust the
margins so that the text continues on next line starting at the same
position as the first character from previous line.

But it is a starting. I will check whether it is possible to place an
image for a bullet. The size and position handling will be still there
then - I think so.

Also note: The tab value from your example has not been accepted (s.th
like. "invalid screen distance")
 
E

eb303

Something like this maybe?
root = Tk()
txt = Text(root, wrap='word')
txt.pack()
txt.tag_configure('text_body', font=('Times', 18), lmargin1=0,
lmargin2=0)
txt.tag_configure('bulleted_list', font=('Times', 18), lmargin1='10m',
lmargin2='15m', tabs=['15m'])
txt.insert(END, u"This is a normal paragraph. Let's make it a bit long
to see that it wraps as expected.\n", 'text_body')
txt.insert(END, u"\u00B7\tThis is the first item in the list.\n",
'bulleted_list')
txt.insert(END, u"\u00B7\tThis is the second item in the list. Let's
make this one quite long too to see how it wraps.\n", 'bulleted_list')

Thank you very much!
However, the result is not that pretty as I have expected. The bullets
are really small. When separating bullet and text then I can increase
the font size for the bullet but then it does not fit to the text -
vertical alignment is wrong. Also it's pretty unhandy to adjust the
margins so that the text continues on next line starting at the same
position as the first character from previous line.

But it is a starting. I will check whether it is possible to place an
image for a bullet. The size and position handling will be still there
then - I think so.

You can also use another font for bullets:
----
from Tkinter import *
import tkFont

root = Tk()

txt = Text(root, wrap='word')
txt.pack()

txt.tag_configure('text_body', font=('Times', 18), lmargin1=0,
lmargin2=0)
txt.tag_configure('bulleted_list', font=('Times', 18), lmargin1='10m',
lmargin2='15m', tabs=['15m'])
txt.tag_configure('bullets', font=('Dingbats', 18))

txt.insert(END, u"This is a normal paragraph. Let's make it a bit long
to see that it wraps as expected.\n", 'text_body')
txt.insert(END, u'\u25C6', 'bullets')
txt.insert(END, u"\tThis is the first item in the list.\n",
'bulleted_list')
txt.insert(END, u'\u25C6', 'bullets')
txt.insert(END, u"\tThis is the second item in the list. Let's make
this one quite long too to see how it wraps.\n", 'bulleted_list')

root.mainloop()
----
Also note: The tab value from your example has not been accepted (s.th
like. "invalid screen distance")

This is probably why you had all these alignment problems. But it's
weird, because the script I posted is copied and pasted from a really
script that I've run, and which doesn't cause any error. What is the
version of tcl/tk used by your Tkinter module? And what is your Python
version?
 
T

Thomas Lehmann

This is probably why you had all these alignment problems. But it's
weird, because the script I posted is copied and pasted from a really
script that I've run, and which doesn't cause any error. What is the
version of tcl/tk used by your Tkinter module? And what is your Python
version?

Using python 2.5 (with Tcl/Tk 8.4):

Traceback (most recent call last):
File "Text1.py", line 10, in <module>
txt.tag_configure('bulleted_list', font=('Times', 18),
lmargin1='10m', lmargin2='15m', tabs=['15m'])
File "E:\Python25\lib\lib-tk\Tkinter.py", line 3066, in
tag_configure
return self._configure(('tag', 'configure', tagName), cnf, kw)
File "E:\Python25\lib\lib-tk\Tkinter.py", line 1188, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: bad screen distance "['15m']"
 
E

eb303

This is probably why you had all these alignment problems. But it's
weird, because the script I posted is copied and pasted from a really
script that I've run, and which doesn't cause any error. What is the
version of tcl/tk used by your Tkinter module? And what is your Python
version?

Using python 2.5 (with Tcl/Tk 8.4):

Traceback (most recent call last):
File "Text1.py", line 10, in <module>
txt.tag_configure('bulleted_list', font=('Times', 18),
lmargin1='10m', lmargin2='15m', tabs=['15m'])
File "E:\Python25\lib\lib-tk\Tkinter.py", line 3066, in
tag_configure
return self._configure(('tag', 'configure', tagName), cnf, kw)
File "E:\Python25\lib\lib-tk\Tkinter.py", line 1188, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: bad screen distance "['15m']"

Try tabs='15m' instead. Seems the list is not handled properly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top