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!

A question back to ulis from ask some advise 1

Status
Not open for further replies.

stewang

Programmer
Aug 21, 2003
77
NZ
Hello ulis:
I am back again, I was busy for other things.
Last time, you said that the file name acturally is a path name, and you put a list a path of filename on the listbox.
Do you have any idea how to put the file name on the listbox, but we just can see the filename, not the path of the filename, and its fuctionality still same as now on the listbox, when people click on it, we still can save it to anther dirctory, or play it, like the MP3 player.
For example, I'd like the file name on the listbox like following;
.Butterfly
.test.cc
not like . ./home/bin/tcl/Butterfly
./home/bin/tcl/test.cc

I tried to search information from the website you gave me, but I still
couldn't solve this problem. Would you mind help me again for this question?

rgds
stewang


And the code from last time

Code:
 package require Tk
  # the menu bar
  menu .mbar
  . configure -menu .mbar
  menu .mbar.file -tearoff 0
  .mbar add cascade -label "File" -underline 0 -menu .mbar.file
  .mbar.file add command -label "Open" -underline 0 -command openFile
  .mbar.file add command -label "Save" -underline 0 -command saveFile
  .mbar.file add separator
  .mbar.file add command -label "Quit" -underline 0 -command exitApp;
  wm protocol . WM_DELETE_WINDOW exitApp
  menu .mbar.list -tearoff 0
  .mbar add cascade -label "List" -underline 0 -menu .mbar.list
  .mbar.list add command -label "Remove" -underline 0 -command removeName
  .mbar.list add command -label "Clear" -underline 0 -command clearList
  # the list box
  set ::names {}
  listbox .l -listvar ::names -width 40
  pack .l -expand 1 -fill both
  # the actions
    # get some file names
    # add the new ones to the listbox
    # do something with the content of each new file
  proc openFile {}   {
    # get some file names
    set filenames [tk_getOpenFile -multiple 1]
    foreach fn $filenames     {
      # check if its a new one
      if {[lsearch $::names $fn] == -1}       {
        # add the name to the listbox
        lappend ::names $fn
        # get the file content and do something with it
        set thefile [open $fn r]
        puts "the file : $thefile"             
        set filecontent [read $thefile]        
        close $thefile
      }
    }
    # sort the listbox content
    set ::names [lsort $::names]
  }
    # get the currently selected name
    # copy the corresponding file somewhere
    # remove the name from the listbox
  proc saveFile {}   {
    # get the currently selected name
    set n [.l curselection]
    if {$n != ""}     {
      # get the old name
      set source [lindex $::names $n]
      # get the new name
      set target [tk_getSaveFile]
      if {$target != ""}       {
        # copy the file content to the new position
        file copy $source $target
        # remove the name from the listbox
        removeName
      }
    }
  }
    # get the currently selected name
    # remove the name from the listbox
  proc removeName {}   {
    # get the currently selected name
    set n [.l curselection]
    set name [lindex $::names $n]
    set n [lsearch $::names $name]
    if {$n != -1}     {
      set ::names [lreplace $::names $n $n]
    }
  }
    # clear the listbox
  proc clearList {} { set ::names {} }
    # exit
  proc exitApp {} { exit }
 
I understand that you want to show the simple file name in the listbox but want to keep the full path name to use it later.

I think that the listbox doesn't have the ability to associate a user string to an item. So you need to make the association outside the listbox. The best way is an associative array. But, for that, you need to have all the simple file names unique (not duplicated).

Under this assumption:
1/ To get file names and associate it an item of the array:
The array is initialized with [array set ::pathes {}].
The simple name is extracted from the full path by [file tail $fullpath].
This is the simple name that is added to the list of the listbox.
The full path is associated to the simple name with [set ::pathes($simplename) $fullpath].
2/ To save the file to another position:
The simple name is retrieved from the list of the listbox and the full path from the array by [$::pathes($simplename)].
3/ To remove an item from the list:
The simple name is retrieved from the list of the listbox and removed. And the associated full path is removed from the array by [unset ::pathes($simplename)].
4/ To clear the list:
The list of the listbox is cleared.
The associated array is emptied by [array unset ::pathes; array set ::pathes {}].

The new code:
Code:
 package require Tk
  # the menu bar
  menu .mbar
  . configure -menu .mbar
  menu .mbar.file -tearoff 0
  .mbar add cascade -label "File" -underline 0 -menu .mbar.file
  .mbar.file add command -label "Open" -underline 0 -command openFile
  .mbar.file add command -label "Save" -underline 0 -command saveFile
  .mbar.file add separator
  .mbar.file add command -label "Quit" -underline 0 -command exitApp;
  wm protocol . WM_DELETE_WINDOW exitApp
  menu .mbar.list -tearoff 0
  .mbar add cascade -label "List" -underline 0 -menu .mbar.list
  .mbar.list add command -label "Remove" -underline 0 -command removeName
  .mbar.list add command -label "Clear" -underline 0 -command clearList
  # the list box
  set ::names {}
  listbox .l -listvar ::names -width 40
  pack .l -expand 1 -fill both
  # the actions
    # get some file names
    # add the new ones to the listbox
    # do something with the content of each new file
  array set ::pathes {}
  proc openFile {}   {
    # get some file names
    set filenames [tk_getOpenFile -multiple 1]
    foreach fn $filenames     {
      # get simple name
      set name [file tail $fn]
      # check if its a new one
      if {![info exists ::pathes($name)]}       {
        # add the name to the array
        puts "set ::pathes($name) $fn"
        set ::pathes($name) $fn
        # add the name to the listbox
        lappend ::names $name
        # get the file content and do something with it
        set thefile [open $fn r]
        puts "the file : $thefile"             
        set filecontent [read $thefile]        
        close $thefile
      }
    }
    # sort the listbox content
    set ::names [lsort $::names]
  }
    # get the currently selected name
    # copy the corresponding file somewhere
    # remove the name from the listbox
  proc saveFile {}   {
    # get the currently selected name
    set n [.l curselection]
    if {$n != ""}     {
      # get the old name
      set source [lindex $::names $n]
      # get the new name
      set target [tk_getSaveFile]
      if {$target != ""}       {
        # copy the file content to the new position
        file copy $::pathes($source) $target
        # remove the name from the listbox
        removeName
      }
    }
  }
    # get the currently selected name
    # remove the name from the listbox
  proc removeName {}   {
    # get the currently selected name
    set n [.l curselection]
    set name [lindex $::names $n]
    set n [lsearch $::names $name]
    if {$n != -1}     {
      set ::names [lreplace $::names $n $n]
    }
    unset ::pathes($name)
  }
    # clear the listbox
  proc clearList {}   { 
    set ::names {}
    array unset ::pathes
    array set ::pathes {}
  }
    # exit
  proc exitApp {} { exit }
HTH

ulis
 
Hi ulis:
Thanks for your time, I will try it later on by following your ideas.

rgds
stewang
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top