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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to catch the events from file dialogs? 2

Status
Not open for further replies.

RajVerma

Programmer
Jun 11, 2003
62
DE
hi all,
I want to create a file(or dir) in some particular directory by using a file dialog, but I'm not able to do it. nothing is getting saved if I click 'save' button from the tk_getSaveFile. how shud I catch the button click event from the buttons in this file dialog?
 
tk_getSaveFile doesn't save any file. Its purpose is to return a convenient file name. Given the returned name you need to save yourself the file:
Code:
  set fn [tk_getSaveFile]
  if {$fn != ""}   {
    set fp [open $fn w]
    puts $fp $data
    close $fp
  }   else   { error "file not saved!" }

From the Tk Manual:
If the user selects a file, both tk_getOpenFile and tk_getSaveFile return the full pathname of this file. If the user cancels the operation, both commands return the empty string.

HTH

ulis
 
thanq ulis.
can anyone give me a simple syntax for getting the index of the number of line in the file. suppose I want to fetch the first string of the 3rd line in the file!! I used the following code.

# This is to fetch the data from the file into the window
set path [format "/xwinnmr/tcl/xwish_scripts/iu_acqu.txt"]
set file [open $path r]
foreach line [split [read $file] \n] {
set index [string range [lindex $line 0] 0 end]
set freq1 [string range [lindex $line 1] 0 end]
set time1 [string range [lindex $line 2] 0 end]
}
close $file

this way it's fetching the strings only from the last line. so wat to do if I want to get acess to the ith line?

thanks in advance.
 
Code:
  set fn myfile
  set fp [open $fn]
  set lines [split [read $fp] \n]
  close $fp
  set line_i [lindex $lines $i]
Read in all lines and then select the one you need!

HTH

ulis

 
Of course, if you've got a really big file, and only need some information from the beginning of the file, you can use the gets command to read a line at a time, stopping once you've reached the data you need. For example:

Code:
set fd [open $myfile r]

# Skip first 2 lines

gets $fd
gets $fd

# Read and save 3rd line

set count [gets $fd line]

A couple more comments about the code you posted, though. First of all, there's no point to the format command on the first line. Without any values to format, you're just going to get your format string returned to you, so it's equivalent to:

Code:
set path "/xwinnmr/tcl/xwish_scripts/iu_acqu.txt"

Second, there's no point to the string range commands in the lines where you're extracting element values with lindex. You're asking string range to return all the characters in the element starting with the first one and going all the way to the end. This is always going to be exactly the same as what lindex is returning, so I don't see why you're doing it. The following accomplishes exactly the same thing:

Code:
set index [lindex $line 0]
set freq1 [lindex $line 1]
set time1 [lindex $line 2]

- 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
 
thanx Ken n Ulis, for improvising the functionality of my code.
Raj.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top