[Tutor] update Tkinter text widget in realtime?

Alan Gauld alan.gauld at btinternet.com
Thu Feb 3 02:17:13 CET 2011


"Elwin Estle" <chrysalis_reborn at yahoo.com> wrote

Caveat: I haven't studied this in detail so I might be
missing something...

> So, just for the heck of it, I thought, what would happen
> if I put a word into the matrix, such that its first letter is
> in the center, and then it rotate around its insertion point.
>
> This sets up a Tkinter text widget and inserts the matrix into it.
>
> I figured I could just clear the widget by deleting everything
> in it, then putting a matrix, modified with the next rotation into 
> it.
>
> No dice.  The rotations are happening, but the text widget
> only materializes after the last rotation, and the final "frame"
> in the rotation is the only one it shows.

Tk is an event driven framework so it will only update between
events. But I don't see any events in your code. You do the
initial population of the matrix and add it to the text widget
but then nothing happens... Where are the events that would
cause the display to update? I'd expect to see a timer or
similar mechanism forcing a redraw of the widgets.

> Any ideas?  The last 10-12 lines are the ones that do
> the rotation.  I've tried moving the root.mainloop() statement
> up higher in the code.  That causes the text widget to show
> up, but no matrix inside.

mainloop() starts the event loop. From that point on Tk is
waiting for events to process. But since you have not
defined any handlers all events are just ignored.

You need to add an event handler (for a timer say) that will
catch timeouts and update the Text widget. It can then
set a new timer ready for the next update.

> root = Tk()
>
> fixed_width = Font(family = 'Courier', size = 10)
> textbox = Text(root, font = fixed_width, width = 40, height = 20)
> textbox.pack()

This sets up the UI widget but no event handlers..

> def blank_matrix(sizeX, sizeY, fillchar):
....
>    return letter_matrix

This fills the matrix

> def print_matrix(matrix):
>    textbox.delete(1.0, END)
>    for line in matrix:
>        #print ' '.join(line)
>        line = ' '.join(line) + '\n'
>        textbox.insert(END, line)


This fills the widget

> def insert_word(word, print_dir, x, y, matrix):
> ...
>    return matrix

More matrix filling

> directions = ['e', 'ne', 'n', 'nw', 'w', 'sw', 's', 'se']
> for direction in directions:
>    print direction
>    matrix = blank_matrix(20, 20, '.')
>    matrix = insert_word('test_word', direction, 10, 10, matrix)
>    print_matrix(matrix)

This goes through the loop once using the functions
above but again creates no event handlers to update the GUI.

> root.mainloop()

Now we are waiting for events but without any handlers
the events that do happen - and with no control widgets
there will not be many - are ignored or defaulted (move,
resize etc).

> I've also doctored the code to get a final matrix with
> all rotations included...so the rotations are for sure happening,
> but the text widget just isn't updating.

Because you are not telling it to update.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list