Tkinter: ability to delete widgets from Text and then re-add them

N

nholtz

Is there any way to delete a widget (window) from a Text widget, and
then add it back
to the Text, without re-creating the original widget. For example, I
think I would like to do
something like the following:
##########################################################
from Tkinter import *

root = Tk()

textWidget = Text( root )
textWidget.pack()

#### create some widgets
widgets = [ Label( textWidget, text=" Hello World #%d! " % (i,) ) \
for i in range(3) ]

#### add all of them to Text
for widget in widgets:
posn = textWidget.index(INSERT)
textWidget.window_create( posn, window=widget )

#### delete the last one from Text
widget - widgets[-1]
textWidget.delete( posn ) # doesn't delete from children of Text
widget.destroy() # this doesn't seem to make any diff, but it does
delete from children

#### try to add it again
textWidget.window_create( posn, window=widget ) #### FAILS !!!

root.mainloop()
###############################################

That last window_create fails with:

_tkinter.TclError: bad window path name ".1076792812.1076865452"

I woule like to create a number of widget objects, then add / subtract
/ re-add them in
a predicatable order (stack-like)

I'm trying to implement 'Wizard-like' behaviour, but in a single Text
window, where
you step through in small chunks, adding new widgets, but are able to
step backwards
by deleteing them. I was hoping not to have to re-create the widgets
again when
moving forwards, as I was hoping for an easy way to maintain the state.

thanks
neal
 
N

nholtz

Sigh. Of course, near the middle is supposed to be:

#### delete the last one from Text
widget = widgets[-1]
 
N

nholtz

I now see that I can accomplish what I want using the Canvas widget,
instead of the
Text widget (I.e., I can create a window on it, delete the window, then
re add it without
recreating the underlying widget object).

As I don't want the raw contents user-editable, I guess its better to
use a Canvas, rather
than a Text (slightly harder to use, though, I guess)

neal
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top