Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clearing text from an entry widget 1

Status
Not open for further replies.

Malachi

Programmer
Aug 13, 2001
38
US
I have an dialog box that appears and asks the user for some information. When the user presses the "SAVE" button, the text is read and stored in an internal data structure. This all works fine.

When the dialog box appears the second time the old text is still visible in the entry widgets. I need to clear each entry widget during some point in my session with the end user.

I know I can use <path_to_entry_widget> delete 1.0 end to clear the entry box. Must I invoke the delete command on every entry widget, or is there a more general command I could use to refresh the dialog data? I would like to clear the entry widgets upon exit of the dialog box. Is there a way to call a procedure upon exit of a dialog box?

Thank you in advance.


 
set the -textvariable option to some variable, say, userdata. Then whatever the user enters in the entry widget is the value, $userdata. Then, after you process the data, set userdata &quot;&quot;.

Bob Rashkin
rrashkin@csc.com
 
As you've discovered, widgets retain their state information, even when they are not displayed on the screen. This is usually a Good Thing, but as you've discovered, can be a little inconvenient when dealing with reusable dialogs. So in answer to your basic question, yes you'll need to clear the entries (and refresh any other widget information) explicitly. You can do this either when the user dismisses the dialog, or immediately before redisplaying it.

Bong also described a nice technique for managing entry contents. The -textvariable option is also a very useful way to retrieve the contents of an entry, rather than using the entry's get subcommand. One technique you could use to minimize the number of global (or namespace) variables when dealing with a lot of textvariables is to use global (or namespace) array elements as the textvariables. For example:

Code:
entry .d1.name -textvariable d1info(name)
entry .d1.address -textvariable d1info(address)
entry .d1.phone -textvariable d1info(phone)

Another advantage of this technique is that you can use array introspection commands to determine the elements you need to reset, rather than hard-coding them. This technique makes it easy to extend your dialog with more entries at a later date, and automatically handle resetting them along with all the other entries. For example:

Code:
proc resetEntries {} {
    global d1info
    foreach elem [array names d1info] {
        set d1info($elem) &quot;&quot;
    }
}

As for actually calling this procedure when the user dismisses the dialog, it's best to simply call it yourself from whatever call handles the user interaction. Presumably, you've already got code called when the user clicks the &quot;Save&quot; button. Just add your call to the reset procedure to that code.

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Thank you both for this information. I am using array elements for the textvariables, but I forgot about &quot;array names <array_name>&quot;. Using a hash to store name/value pairs is a trick I learned when writing Perl CGI scripts for parsing HTML forms.

Since all entry widgets need to be reset, I was thinking of using an array for each of the entry widget names, and then pass that array to my &quot;reset&quot; procedure. I could walk through each array element and delete the text.

Would someone have an explanation for another problem I'm having? If I use linsert to insert values into a list out of order: element 0, element 1, element 3, element 2, I noticed that element 3's value is &quot;stomped on&quot;. I'm unable to retrieve the list value for element 3 using lindex. If I build a list with linsert and add the elements in order: element 0, element 1, element 2, element 3, then I can retrieve all elements using lindex.

Thanks again!

Malachi

 
Hm. linsert shouldn't work that way. Could you post a brief example illustrating the problem you're encountering? We'll probably be able to diagnose the problem from that.

Here's an example from an interactive session demonstrating linsert:

[tt]% [ignore]set vals {first second}[/ignore]
first second
% [ignore]set vals [linsert $vals 1 third][/ignore]
first third second
% [ignore]set vals [linsert $vals 0 fourth fifth][/ignore]
fourth fifth first third second[/tt]

As an aside, for building lists &quot;in order,&quot; are you aware of the lappend command? It takes as its first argument the name of a variable. All additional arguments are appended as separate elements to the end of the list stored in the variable. (It's the only list command that actually modifies the value stored in a variable.) For example:

[tt]% puts $states ;# This variable doesn't exist yet
can't read &quot;states&quot;: no such variable
% lappend states California
California
% lappend states Hawaii
California Hawaii
% lappend states Indiana &quot;New York&quot;
California Hawaii Indiana {New York}
% llength $states
4[/tt]

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top