471,338 Members | 1,063 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

clearing text on canvas

hi
i am new to tkinter and would like some help with canvas

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:
resultdisplay.create_text(1,50,anchor=W,text=errme ssage,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?
dn

Dec 18 '07 #1
2 4800
de****@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=errme ssage,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
Dec 19 '07 #2
thanx a lot Peter
dn
Dec 20 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Gary Richardson | last post: by
4 posts views Thread by Peter Otten | last post: by
2 posts views Thread by Billy | last post: by
6 posts views Thread by mphanke | last post: by
4 posts views Thread by Dragon | last post: by
5 posts views Thread by deacon.sweeney | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.