after idle { namespace eval ::MyFileBrowser:: { variable w ".myfilebrowser"; # handle to base widget variable molid -1; # molid of molecule to load into. variable moltxt "New Molecule"; # Name of molecule to load into. variable typeid 0; # index of file type. variable typelist {auto pdb dcd cube hoomd} ; # has to match list below. variable filetypes {Automatic {PDB File} {DCD file} {Gaussian Cube} {HOOMD XML}} proc loadfile {} { variable w variable molid variable b variable typeid variable typelist set fname [tk_getOpenFile -title {Load File...} -parent $w] if {! [string length $fname] } return ; # user has canceled file selection. if {$molid < 0} { if {$typeid > 0} { set molid [mol new $fname type [lindex $typelist $typeid] waitfor all] } else { set molid [mol new $fname waitfor all] } } else { if {$typeid > 0} { mol addfile $fname type [lindex $typelist $typeid] waitfor all molid $molid } else { mol addfile $fname waitfor all molid $molid } } return } # update molecule list proc updatemolecule {args} { variable w variable molid global vmd_molecule # Update the molecule browser $w.from.mol.m.menu delete 0 end $w.from.mol.m configure -state disabled $w.from.mol.m.menu add radiobutton -value "-1" \ -label "New Molecule" \ -variable [namespace current]::molid $w.from.mol.m.menu add separator set mollist [molinfo list] foreach id $mollist { $w.from.mol.m.menu add radiobutton -value $id \ -label "$id [molinfo $id get name]" \ -variable [namespace current]::molid } $w.from.mol.m configure -state normal } proc gui {} { variable w variable molid variable filetypes catch {destroy $w} toplevel $w wm title $w "My Custom File Loader" wm iconname $w "Custom File Loader" wm minsize $w 200 30 # Molecule selector frame $w.from frame $w.from.mol label $w.from.mol.l -text "Load into Molecule:" -anchor w menubutton $w.from.mol.m -relief raised -bd 2 -direction flush \ -textvariable [namespace current]::molid -menu $w.from.mol.m.menu menu $w.from.mol.m.menu -tearoff no pack $w.from.mol.l -side left pack $w.from.mol.m -side right pack $w.from.mol -side top pack $w.from -side left # File type selector frame $w.to frame $w.to.mol label $w.to.mol.l -text "File Type:" -anchor w menubutton $w.to.mol.m -relief raised -bd 2 -direction flush \ -textvariable [namespace current]::typeid -menu $w.to.mol.m.menu menu $w.to.mol.m.menu -tearoff no pack $w.to.mol.l -side left pack $w.to.mol.m -side right pack $w.to.mol -side top pack $w.to -side left set i 0 foreach t $filetypes { $w.to.mol.m.menu add radiobutton -value $i \ -label $t -variable [namespace current]::typeid incr i } # loading button $w.foot -text {Load File...} -command [namespace code loadfile] pack $w.foot -side bottom -fill x grid columnconfigure $w.foot 0 -weight 2 -minsize 10 grid columnconfigure $w.foot 1 -weight 4 -minsize 10 # layout grid config $w.from -column 0 -row 0 -columnspan 1 -rowspan 1 -sticky "snew" grid config $w.to -column 1 -row 0 -columnspan 1 -rowspan 1 -sticky "snew" grid config $w.foot -column 0 -row 1 -columnspan 2 -rowspan 1 -sticky "snew" grid columnconfigure $w 0 -weight 2 -minsize 150 grid columnconfigure $w 1 -weight 1 -minsize 150 updatemolecule global vmd_molecule trace variable vmd_molecule w [namespace current]::updatemolecule } } # callback for VMD menu entry proc myfilebrowser_tk_cb {} { ::MyFileBrowser::gui return $::MyFileBrowser::w } # register and show if {[catch {menu tk register myfilebrowser myfilebrowser_tk_cb "My File Browser"} msg]} { vmdcon -error "The myfilebrowser window could not be created:\n$msg" } else { menu myfilebrowser on } }