clearing text on canvas
Peter Otten
__peter__ at web.de
Wed Dec 19 03:47:58 EST 2007
devnew at gmail.com wrote:
> i am doing validation of contents of a folder and need to show the
> ok/error messages on a canvas
>
> resultdisplay =Canvas(...)
> errmessage="error!"
> okmessage="dir validation ok!"
>
> if dirvalidate is False:
if ... is False: ...
is bad style. Just
if dirvalidate: ...
reads better and is less likely to cause subtle errors.
> resultdisplay.create_text(1,50,anchor=W,text=errmessage,width=175)
>
> else:
> self.resultdisplay.create_text(1,50,anchor=W,text=okmessage,width=175)
>
>
> my problem is that if validation succeeds or fails the text created on
> canvas is displayed over the previous created text
> I need to clear the previous text from the canvas before creating new
> text
> can someone help?
You create the text once but keep its handle for further changes.
handle = canvas.create_text(...)
You can change it later with
canvas.itemconfigure(handle, ...)
Here's a short self-contained example:
import Tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=100, height=100)
canvas.pack()
text_id = canvas.create_text(50, 50, width=100)
ok = False
def change_text():
global ok
ok = not ok
if ok:
text = "ok"
else:
text = "error"
canvas.itemconfigure(text_id, text=text)
button = tk.Button(root, text="change text", command=change_text)
button.pack()
root.mainloop()
Peter
More information about the Python-list
mailing list