TCL/tk problem in Shen/tk graphics

36 views
Skip to first unread message

dr.mt...@gmail.com

unread,
May 13, 2024, 7:24:47 AMMay 13
to Shen
I used to keep this stuff off this group, but since the main TCL/tk group has been killed,
there's no obvious group to go to.  Somebody with enough TCL/tk knowledge might be able to solve this one.

This program reads a TCL/tk command from a file.  For example:

button .b eot

The eot (end of transmission) shows the command is ready to be executed.   The function newcommand? tests the file for a new command

proc newcommand? {File} {
  set Source [open $File r]
  set Data [read $Source]
  set Verdict [eot? $Data]
  close $Source
  return $Verdict}

proc eot? {S} {
   return [ string match *eot $S ]
   } 

So I put  button .b eot  into the file which is called in in the program.  I ask TCL if there is a new command waiting.

(bin) 23 % newcommand? $in
1


TCL/tk says yes.  I then tell TCL to enact what is in this file.

proc enact {File} {
  set Source [open $File r]
  set Data [read $Source]
  set Command [trim $Data]
  overwrite $File
  eval $Command
  close $Source}


trim just trims the eot off the file contents.  So I enter enact $in.  The contents are eaten and the in file is now empty.  If I then submit

pack .b eot

in the same way; sure enough the button appears in WISH.  Now I enter

set myvar 1

and enact that.   But if I then enter 

puts $myvar

I get 

can't read "myvar": no such variable

Mark

Mark Tarver

unread,
May 13, 2024, 7:37:38 AMMay 13
to qil...@googlegroups.com
Slightly better is to close the first stream before overwriting the file

proc enact {File} {
  set Source [open $File r]
  set Data [read $Source]
  close $Source
  set Command [trim $Data]
  overwrite $File
  eval $Command
  }


proc overwrite {File} {
  set Sink [open $File w]
  puts -nonewline $Sink ""
  flush $Sink
  close $Sink}

However it has no effect on the problem.

Mark

--
You received this message because you are subscribed to the Google Groups "Shen" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qilang+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/qilang/cd740512-3af5-40a9-b0db-ea7651db313bn%40googlegroups.com.

Bruno Deferrari

unread,
May 13, 2024, 8:51:27 AMMay 13
to qil...@googlegroups.com
It is not clear to me where you are calling "set myvar", is that code that would be processed by the eval call?

It is probably being evaluated in a different environment than the "puts $myvar" you have later.

I found https://www.tcl-lang.org/man/tcl8.6/TclCmd/global.htm, so after the `set myvar` (or maybe before) you could try "global myvar"



--
BD

Bruno Deferrari

unread,
May 13, 2024, 8:54:47 AMMay 13
to qil...@googlegroups.com
This may be useful too (to avoid the global scope and run your evals in a controlled context):

--
BD

Mark Tarver

unread,
May 13, 2024, 8:55:19 AMMay 13
to qil...@googlegroups.com
Yes; I was just going out of the door and it hit me before 
I read your post.  The set myvar is being read locally
and probably the variable only exists within the enact
procedure.   It;s a bit of an odd case because the 
variable isn't cited in the procedure itself but TCL/tk has
decided to make it local.

There's probably a command to force variables to be global
but right now the sun is shining and I'm out.

Mark

You received this message because you are subscribed to a topic in the Google Groups "Shen" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/qilang/ZWdo7QOClq0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to qilang+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/qilang/CACnPB4psZ_i%2Bf8xXppL5fdicK1V%2BbpVwFs-UaGRMoVFoTfRxpA%40mail.gmail.com.

Bruno Deferrari

unread,
May 13, 2024, 10:27:46 AMMay 13
to qil...@googlegroups.com
Btw, as an alternative to the mailing lists, you can try those AI chatbots, they are usually quite good at answering programming questions. Free versions are usually limited in time or amount of queries, but that is usually more than good enough for cases like this.

Not sure which one is the best these days, but a few I have tried are:


and on this page you can try two models at the same time to compare the answers:

https://arena.lmsys.org/



--
BD

dr.mt...@gmail.com

unread,
May 14, 2024, 2:31:32 AMMay 14
to Shen
Thanks.  TCL/tk seems to resist
using the global declaration in this 
context because the variables are
dynamically generated.  This only
affects one feature of Shen/tk which
is the ability to call down web pages.  
There is a workaround for this I think
which might work.

M.

dr.mt...@gmail.com

unread,
May 14, 2024, 5:19:13 AMMay 14
to Shen
I still have a technical TCL/tk problem here and its gone to Stackoverflow.


for those who might have an answer.

Mark

Reply all
Reply to author
Forward
0 new messages