newlisp-tk Entry widget does not display textvariable

Started by jp, March 20, 2004, 10:10:26 PM

Previous topic - Next topic

Lutz

#15
You could do the following:



(tk "set ::display")





or any other Tcl/Tk statement which would return the contents of the variable. This would return the contents of the entry widget in a string.



Read the chapter about "Writing applications with newLISP and Tcl/Tk" in the manual.



Lutz

HPW

#16
>How could you send back the content of the entry in ::display to newLisp?



I think he means how to push it back from the GUI to newLISP.

The we must bind an TK event (for eample return key in entry widget) to a proc, where we call a function in newLISP.



A look into newLISP-tk.tcl from the source may be usefull.
Hans-Peter

Lutz

#17
the demo examples shipped with the Windows version also show how to bind events to buttons, mouse etc.



Lutz

Lutz

#18
here is a comprehensive example for an entry widget:



(define (entry )
  (tk "if {[winfo exists .entry] == 1} {destroy .entry}")
  (tk "toplevel .entry")
  (tk "wm title .entry {entry example}")
  (tk "entry .entry.e -width 30 -textvariable ::display")
  (tk "bind .entry.e <Return> { Newlisp {(silent (get-entry-text))} }")
  (tk "pack .entry.e"))

(define (get-entry-text )
  (println (tk "set ::display")))


Whenever you hit the return/enter key you will see the contents of the entry widget displayed in the console. There are many other events you can bind to Tcl/Tk widget, get a book, i.e.: "Practical Programming in Tcl and Tk" by Brent Welch.



Lutz

gerhard zintl

#19
Hallo Lutz,

I tested your 'entry example' and it runs like a charm.

Thank you very much for your prompt help.

I have some experience with Tcl/Tk and also Brent Welch's book fourth ed.

My problem was the data transfer connection between TK and newLISP

Gerhard Zintl

jp

#20
listbox widget example

jp

#21
Lutz gave a comprehensive entry widget example.

Here is listbox widget example.

It is a file chooser, where one has simply to click a given file to either open or run it. One can easily extend it to make newlisp to distinguish directories and files and navigate drives and make newlisp to do all sorts of what not!

I choose to use start.exe from win98 instead of the internal command assoc since that command exists only on Win2k and WinXP.



Jean-Pierre



------------------------- fc.lsp



(define (listbox)

(tk

[text]

if {[winfo exists .fc] == 1} {destroy .fc}

toplevel .fc

set ::result ""

wm title .fc {File Chooser}

frame .fc.fr -borderwidth 10

pack  .fc.fr -side top -expand yes -fill y



scrollbar .fc.fr.yscroll -orient vertical   -command ".fc.fr.list yview"

scrollbar .fc.fr.xscroll -orient horizontal -command ".fc.fr.list xview"

listbox .fc.fr.list -width 20 -height 10 -setgrid 1 -yscroll ".fc.fr.yscroll set" -xscroll ".fc.fr.xscroll set"



button .fc.b -text {Exit All} -command exit

label  .fc.lb -textvariable ::result

pack   .fc.lb .fc.b -side top -fill x



grid .fc.fr.list -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news

grid .fc.fr.yscroll -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news

grid .fc.fr.xscroll -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news

grid rowconfig    .fc.fr 0 -weight 1 -minsize 0

grid columnconfig .fc.fr 0 -weight 1 -minsize 0



foreach i [lsort [glob *]] {

  .fc.fr.list insert end $i

}



proc fiche {} {

 set entry [open tmp w]

 puts  $entry $::result

 close $entry

}



bind .fc.fr.list <ButtonRelease-1> {

  set ::result [selection get]

  set ext [string tolower [file extension $::result]]

  switch -glob -- $ext {

    ""      { exec notepad $::result & }

    ".bat"  { exec notepad $::result & }

    ".ini"  { exec notepad $::result & }

    ".sys"  { exec notepad $::result & }

    ".lsp"  { fiche ; Newlisp {(entry)} }

    default { exec c:/dos/start $::result & }

  }

}

[/text]

))



(define (entry)

 (eval (append '(load) (parse (read-file "tmp"))))

 (delete-file "tmp"))



--------------------- end of code

jp

#22
Easier and little bit faster



Jean-Pierre



--------------------------- fx.lsp



(define (fx)

(tk

[text]

if {[winfo exists .fx] == 1} {destroy .fx}

toplevel .fx

wm title .fx {File Chooser}

frame .fx.fr -borderwidth 10

pack  .fx.fr -side top -expand yes -fill y



scrollbar .fx.fr.yscroll -orient vertical   -command ".fx.fr.list yview"

scrollbar .fx.fr.xscroll -orient horizontal -command ".fx.fr.list xview"

listbox .fx.fr.list -width 20 -height 10 -setgrid 1 -yscroll ".fx.fr.yscroll set" -xscroll ".fx.fr.xscroll set"



button .fx.b -text {Exit All} -command exit

label  .fx.lb -textvariable ::result

pack   .fx.lb .fx.b -side top -fill x



grid .fx.fr.list -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news

grid .fx.fr.yscroll -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news

grid .fx.fr.xscroll -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news

grid rowconfig    .fx.fr 0 -weight 1 -minsize 0

grid columnconfig .fx.fr 0 -weight 1 -minsize 0



foreach i [lsort [glob *]] {

  .fx.fr.list insert end $i

}



bind .fx.fr.list <ButtonRelease-1> {

  set ::result [selection get]

  set ext [string tolower [file extension $::result]]

  switch -glob -- $ext {

    ""      { exec notepad $::result & }

    ".bat"  { exec notepad $::result & }

    ".ini"  { exec notepad $::result & }

    ".sys"  { exec notepad $::result & }

    ".lsp"  { Newlisp {(entry)} }

    default { exec c:/dos/start $::result & }

  }

}

[/text]

))



(define (entry) (eval (append '(load) (list (tk "set ::result")))))



-------------------- end of code

Lutz

#23
Thanks for the comprehensive listbox example, a nice piece showing how to use the grid manager in Tk organizing several widgets.



Note that Tcl/Tk also has some built-in dialogs already, some of them for files too, try these:



(tk "tk_chooseColor")

(tk "tk_messageBox -message {Hello World!}")

(tk "tk_getOpenFile")

(tk "tk_getSaveFile")



All of these have many options but work in their minimalistic form shown here with useful return values.



Lutz

jp

#24
Thanks Lutz



Perhaps all the outstanding features of the newlisp tk interface should be mentioned in the newlip-tk documentation to ease the interfacing and to prevent the inadvertent 'reinventing of the wheel'.



Jean-Pierre

Lutz

#25
Those functions I mentioned are standard Tcl/Tk functions. In the 8000rc1 release I added some more explanations about handling variables with the global prefix ::.



Unfortunately I do very little Tcl/Tk stuff myself, so I depend on users like you pointing out specific problems they run into when interfacing with newLISP. This is how the 'scale' example got in to the manual showing how to work around a specific callback problem.



Before you told us that the :: prefix could be used, I didn't even know about it and tried to solve the problem doing a 'global varName' in each tk statement, which also works, but is kind of clumsy. I have the :: - technique now included in the manual.



Lutz