Tkinter - Text - bullets

Discussion in 'Python' started by Thomas Lehmann, Sep 18, 2009.

  1. 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)
    Thomas Lehmann, Sep 18, 2009
    #1
    1. Advertising

  2. Thomas Lehmann

    eb303 Guest

    On Sep 18, 11:57 am, Thomas Lehmann <Iris-und-Thomas-Lehm...@T-
    Online.de> wrote:
    > 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()
    ----

    HTH
    eb303, Sep 18, 2009
    #2
    1. Advertising

  3. > 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")
    Thomas Lehmann, Sep 19, 2009
    #3
  4. Thomas Lehmann

    eb303 Guest

    On Sep 19, 5:53 pm, Thomas Lehmann <Iris-und-Thomas-Lehm...@T-
    Online.de> wrote:
    > > 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.


    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?
    eb303, Sep 21, 2009
    #4
  5. > 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']"
    Thomas Lehmann, Sep 22, 2009
    #5
  6. Thomas Lehmann

    eb303 Guest

    On Sep 22, 7:45 am, Thomas Lehmann <Iris-und-Thomas-Lehm...@T-
    Online.de> wrote:
    > > 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.
    eb303, Sep 22, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob H

    Datagrid and bullets

    Bob H, Feb 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    396
    =?Utf-8?B?QXZuZWVzaA==?=
    Feb 5, 2004
  2. ComputerSnack
    Replies:
    7
    Views:
    1,869
    Jonathan N. Little
    Apr 13, 2006
  3. babz
    Replies:
    0
    Views:
    450
  4. Paul Lautman

    List item bullets

    Paul Lautman, Dec 28, 2006, in forum: HTML
    Replies:
    15
    Views:
    807
    Jonathan N. Little
    Dec 31, 2006
  5. knowledgenotebook

    Text formatting (bullets) issue with IE7 +

    knowledgenotebook, May 27, 2011, in forum: Javascript
    Replies:
    0
    Views:
    106
    knowledgenotebook
    May 27, 2011
Loading...

Share This Page