0

I want to get file name from gui. I wrote a code like below.

I am able to run all the scripts for once time. what i am looking for when i select the file form hwtk::openfileentry, i want to set a variable for the filename.

After i selected the file, if i run "set filename [$win.op1 get]" i am able to get the name. However i need automate this, i want to trigger the varible after selecting file from gui.

Best Regards

set win .window
catch {destroy $win}
toplevel $win -class TopClass
wm title $win "Tools"
wm geometry $win 420x200+100+150
wm resizable $win 1 1 
wm deiconify $win

label $win.01 -text "openmodel"
place $win.01 -x 0 -y 10 -width 130 -height 32
hwtk::openfileentry $win.op1 
place $win.op1 -x 135 -y 10 -width 250 -height 32
set filename [$win.op1 get]
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

1 Answers1

0

According to the documentation, hwtk::openfileentry takes a -command option to specify a user callback script. I would imagine, based on normal usage for things Tk, that that might the place to trigger reading from the widget and writing to the variable:

proc updateTheFilename {window variable} {
    # Double quotes just because of Stack Overflow's highlighting
    upvar "#0" $variable v
    set v [$window get]
}

# Forming callbacks like this is very much a best practice; least surprising in complex scenarios
hwtk::openfileentry $win.op1 -command [list updateTheFilename $win.op1 filename]
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215