Hello all:
In my application, I'd like to add a special condition to my
file application that if we load and save a special file which is "mylibrary.txt"(just do special thing for this file), I will do something special for it, If I load or save the rest of file, they just do the things as they do in my code now.
In the "mylibray.txt", it contains some a format as following;
<songtitle>|<artist>|albumtitle>|<bitrate>|<duration second>
for example
Is This It|The Strokes|Is This It?|154462|235
Alone Together|The Strokes|Is This It?|192391|246
Last Night|The Strokes|Is This It?|197590|244
what special thing for "mylibray.txt" file, if I load or save it ?
When I load the mylibrary, I need to put all the songtitle in the listbox. I already implement it. which is in readtext function.
when I save them, I must conform the fotmat as above.
for example, if the listbox has some song and other files
as following;
Last Night
Alone Together
test.tk
if I click on the Last Night and save it into a text file,
then we can see a string must look like "Last Night|The Strokes|Is This It?|197590|244"
Could anyone help me with saving the text file, please. I have not idea how to do this.
Thanks
rgds
stewang
In my application, I'd like to add a special condition to my
file application that if we load and save a special file which is "mylibrary.txt"(just do special thing for this file), I will do something special for it, If I load or save the rest of file, they just do the things as they do in my code now.
In the "mylibray.txt", it contains some a format as following;
<songtitle>|<artist>|albumtitle>|<bitrate>|<duration second>
for example
Is This It|The Strokes|Is This It?|154462|235
Alone Together|The Strokes|Is This It?|192391|246
Last Night|The Strokes|Is This It?|197590|244
what special thing for "mylibray.txt" file, if I load or save it ?
When I load the mylibrary, I need to put all the songtitle in the listbox. I already implement it. which is in readtext function.
when I save them, I must conform the fotmat as above.
for example, if the listbox has some song and other files
as following;
Last Night
Alone Together
test.tk
if I click on the Last Night and save it into a text file,
then we can see a string must look like "Last Night|The Strokes|Is This It?|197590|244"
Could anyone help me with saving the text file, please. I have not idea how to do this.
Thanks
rgds
stewang
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
# 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]
if { $name == "mylibrary.txt" } {
readTextFile $fn
return
}
# 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
}
}
}
# read a textfile
proc readTextFile { filenames } {
set thefile [open "mp3library.txt" r]
set line [gets $thefile]
while {[eof $thefile] == 0} {
puts $line
# split the line as before
set data [split $line "|"]
# we know the name is the first element of the list
# resulting from split
set songnametext [lindex $data 0]
if {![info exists ::pathes($songnametext)]} {
set ::pathes($songnametext) $filenames
# add the songnametext to the listbox
lappend ::names $songnametext
# we want the rest of the list without the name
# lrange allows us to pick values between two indices
# note that end is a shortcut for [llength $data] - 1
set songInfor [lrange $data 1 end]
puts "songInfor :$songInfor "
# data associated with an array key can be a complex structure
set myarray($songnametext) $songInfor
set line [gets $thefile]
}
# sort the listbox content
set ::names [lsort $::names]
}
}