Now that we have an interface it would be nice to have records. To accomplish this, we need to give the user the ability to add a record including a photo if desired. This will require that we set up our variables to support adding a record, clear the interface, associate a picture to the entry and then save the record when the user is complete as well as a procedure to load the record for display.
In the address book file, enter the following text at the location defined in our main page for procedures as defined within the comments:
proc addRecord {} { global currentRecord global recordCount global addressInfo global pictureFile # Clear current entries clearRecord set pictureFile "" set currentRecord [expr $recordCount +1] } # Clear any contents from the entry widgets proc clearRecord {} { .main.efirst delete 0 end .main.elast delete 0 end .main.eaddress delete 0 end .main.ecity delete 0 end .main.estate delete 0 end .main.ezip delete 0 end .main.ephone delete 0 end .main.picture configure -image "" } proc getPicture {} { global currentRecord global addressInfo global pictureFile set types { {{GIF} {.gif} } {{PPM} {.ppm} } {{All Files} * } } set filename [tk_getOpenFile -filetypes $types] if {$filename != ""} { # Now that we have the path to the desired picture # we copy it to the working directory # Get the destination filename # by splitting the fullpath into # elements and retrieving the last set listFile [file split $filename] set listCount [llength $listFile] set listIndex [expr $listCount - 1] set pictureFile [lindex $listFile $listIndex] # If the pictureFile already exists within the # working directory we do not copy it again if { [file exists $pictureFile] == 0 } { # Copy the image to the current directory if {[catch {file copy -force "$filename" $pictureFile} sError]} { tk_messageBox -message "File Copy Error $filename to $pictureFile" } } # Update the dictionary entry dict set addressInfo $currentRecord PHOTO $pictureFile # Update the image onscreen image create photo newPicture -file $pictureFile .main.picture configure -image newPicture } } proc saveRecord {} { global currentRecord global recordCount global addressFile global addressInfo global pictureFile # No Records Exist if { $currentRecord == 0 } { incr currentRecord incr recordCount } dict set addressInfo $currentRecord ID "$currentRecord" dict set addressInfo $currentRecord FNAME "[.main.efirst get]" dict set addressInfo $currentRecord LNAME "[.main.elast get]" dict set addressInfo $currentRecord ADDRESS "[.main.eaddress get]" dict set addressInfo $currentRecord CITY "[.main.ecity get]" dict set addressInfo $currentRecord STATE "[.main.estate get]" dict set addressInfo $currentRecord ZIP "[.main.ezip get]" dict set addressInfo $currentRecord PHONE "[.main.ephone get]" dict set addressInfo $currentRecord PHOTO "$pictureFile" # Write the records to the file set fp [open $addressFile w+] dict for {id info} $addressInfo { dict with info { # Create an entry for the file set data "$ID,$FNAME,$LNAME,$ADDRESS,$CITY,$STATE,$ZIP, $PHONE,$PHOTO " # Write the entry to the file puts -nonewline $fp $data } } set recordCount [dict size $addressInfo] # Close the file close $fp } # This procedure is used to load and display a record # from the dictionary proc loadRecord { } { global currentRecord global addressInfo .main.efirst insert 0 [dict get $addressInfo $currentRecord FNAME] .main.elast insert 0 [dict get $addressInfo $currentRecord LNAME] .main.eaddress insert 0 [dict get $addressInfo $currentRecord ADDRESS] .main.ecity insert 0 [dict get $addressInfo $currentRecord CITY] .main.estate insert 0 [dict get $addressInfo $currentRecord STATE] .main.ezip insert 0 [dict get $addressInfo $currentRecord ZIP] .main.ephone insert 0 [dict get $addressInfo $currentRecord PHONE] # Load the photo if {[dict get $addressInfo $currentRecord PHOTO] > 0} { set pictureFile [dict get $addressInfo $currentRecord PHOTO] image create photo addressPhoto -file $pictureFile .main.picture configure -image addressPhoto } }
First, we configured our counters (record and total) and then cleared the screen. This is in preparation for the user to add some data. Once the user selects the Save button, we add the contents of the entry widgets to the dictionary and write them to the data file for future use. Then we display the record. Note that if the user simply changes a record and clicks on Save, he will be performing an edit of the existing record.
18.227.102.50