Why does info exists always return 0 when checkin for the existence of a canva object (Tcl/Tk 8.5)

user3881792

I tried the following test (see snippet hereunder):

and it always returns 0 regardless of the fact that the canvas object (the rectangle in this case) has been created (with "create") or deleted (with "delete").

Is there another way to check if a canvas object exists or has successfully been deleted in Tcl/Tk ?

Thanks !

Serge Hulne

    ############

    tk::canvas .can
    set r1 [.can create rect 30 10 120 80  -outline #fb0 -fill #fb0]
    set r2 [.can create rect 150 10 240 80 -outline #f50 -fill #f50]
    set r3 [.can create rect 270 10 370 80 -outline #05f -fill #05f]
    pack .can


    ##
    ##info exists does not work for canvas elements : it always returns 0
    set rcc [info exists $r2]
    puts "rcc = $rcc"


    if {[info exists $r2]} {
        puts "$r2 exists !"
       .can delete $r2
        puts [info exists $r2]
     } else {
         puts "$r2  does not exist !"
         set rc [info exists $r2]
         puts "rc = $rc  "
    }
    ##
    ##        
    wm title . "colors"
    wm geometry . 400x100+300+300

    ##########
Jerry

info exists is used to check whether a variable exists. A canvas widget is not a variable, it is a widget.

As such, you need to use winfo exists to check whether a canvas exists:

winfo exists .can

But then again, what you used as r2 in your code is an item within the canvas, and there is no command to check its existence as far as I know. However, you can use .can find all to get a list of all the items in the canvas and compare it to r2:

% tk::canvas .can
% set r1 [.can create rect 30 10 120 80  -outline #fb0 -fill #fb0]
% set r2 [.can create rect 150 10 240 80 -outline #f50 -fill #f50]
% set r3 [.can create rect 270 10 370 80 -outline #05f -fill #05f]

% puts $r2
2  # Because it is the second item created
% .can find all
1 2 3

You can then do something like lsearch -exact [.can find all] $r2 and if you get something not -1, know that this item exists.


Though if you use unique tags with your items, you can use .can find withtag to verify its existence:

% tk::canvas .can
% set r1 [.can create rect 30 10 120 80  -outline #fb0 -fill #fb0 -tags t1]
% set r2 [.can create rect 150 10 240 80 -outline #f50 -fill #f50 -tags t2]
% set r3 [.can create rect 270 10 370 80 -outline #05f -fill #05f -tags t3]

% .can find withtag t2
2  # which is equal to $r2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does this always return 0?

Python: Why does the value always return 0

Noob Question! Why does this always return 0?

Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't contain 0?

Why does SocketCAN bind() call always return 0 when called from JNI?

Why does the -e file existence test always return false for a filename containing non-ASCII characters?

Why does (0 < 5 < 3) return true?

Why does this function always return either 0 or 1

Why does "n&1 == 0" always return false?

Why does sudo -n on Mac OS X always return 0?

Why does `time.Since(start).Seconds()` always return 0?

Why does my Excel COUNTIF function always return a value of "0"?

Why does this method always produce 0 as its return value?

Why does my Perl calculator always return 0?

Why does my LINQ query always return 0?

Why does conversion to extFloat80_t always return 0?

Why does this part of my code always return the value 0?

Why does this sql snippet return 8 or 1 always?

why does line() not always return a line-object? why does it sometimes seem to return a double?

Why does islower always return true even when there are digits?

When connecting to Java socket, why does getInetAddress() return /0:0:0:0:0:0:0:1?

Why does this not return 0

Why does Entity Framework always get the latest version of data from the database, but then not use it if the object already exists in the context?

Why does 0L not equal 0 when cast to an object?

Why does Akka not return address/host info?

Why does [:lower:] return differently in bash depending on the existence of files?

why does onActivityResult() return resultCode=0 when user clicks OK?

Why does my SQL query return no rows, when sum is 0

Why does this function return 0 even when assigned a value? (C)