Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello: Would anyone help me to fin 2

Status
Not open for further replies.

stewang

Programmer
Aug 21, 2003
77
NZ
Hello:
Would anyone help me to find the bug,please.
The compiler report to me that my code has invalid command name, but i check the command, i think they are fine, nothing wrong with that.

my code as follow;
Code:
frame .frame1 -background white
label .frame1.label -text  "I am a label"   

# create the button. disable it when it is clicked 
button .frame1.button1 -text  "Format label"  
               -command { .frame1.button1 configure -state disabled createDialog } 

pack .frame1.label .frame1.button1 -side top 
pack .frame1 -fill both -expand yes


proc createDialog {}{

toplevel .dialog 

    radiobutton .dialog.green -text  "green"  
         -variable colour -value green 
         -command { .frame1.label configure -bg $colour } 

    radiobutton .dialog.red -text  "red" 
          -variable colour -value red 
         -command { .frame1.label configure -bg $colour } 
     button .dialog.close -text  "close window"  
         -command { .frame1.button configure -state normal destroy .dialog }
     set colour white 
     pack .dialog.green .dialog.red .dialog.close -side top
}
 
Your problem is the new lines.
You can continue a wide command on the next line if you add a back slash at the end of the line:
Code:
    radiobutton .dialog.green -text  "green"           -variable colour -value green          -command { .frame1.label configure -bg $colour }
Without that Tcl has no way to know if the followin line is a new command or the continuation of the previous one.

For more, see the endekalogue:
Particulary the rules 1 (that states that a newline is a command separator) and 8 (that states that the sequence \nSpace is replaced by a space in a pre-pass)

HTH

ulis
 
Thanks ,the invaild command has gone, but why it still complains that I has extra character?

Code:
frame .frame1 -background white
label .frame1.label -text  "I am a label"   

# create the button. disable it when it is clicked 
button .frame1.button -text  "Format label"                -command { .frame1.button configure -state disabled createDialog } 

pack .frame1.label .frame1.button -side top 
pack .frame1 -fill both -expand yes


proc createDialog {}{

toplevel .dialog 

    radiobutton .dialog.green -text  "green"\  
          -variable colour -value green            -command { .frame1.label configure -bg $colour } 

    radiobutton .dialog.red -text  "red"           -variable colour -value red            -command { .frame1.label configure -bg $colour } 
     button .dialog.close -text  "close window"        -command { .frame1.button configure -state normal destroy .dialog }
     set colour white 
     pack .dialog.green .dialog.red .dialog.close -side top
}
 
0/ Start here:
1/ proc command takes 3 arguments and you gave it only 2:
Code:
  proc createDialog {}{...}
Back to the endekalogue: words are separated with space (& alliens). And {}{...} is only one word.

2/ Back to the endekalogue, the commands are separated by new lines or ;.
So
Code:
.frame1.button configure -state disabled createDialog
is one command. And the frame1.button command complains that createDialog is an unknown option.
You need to add a ; between the two commands:
Code:
  button .frame1.button -text  "Format label"     -command { .frame1.button configure -state disabled; createDialog }
A hint: don't make inline callbacks but create procs:
Code:
  proc formatLabel {}   {
    .frame1.button configure -state disabled
    createDialog 
  }
  button .frame1.button -text "Format label" -command {formatLabel}

3/ Back to the endekalogue: the sequence \nSpace is replaced by a single space outside litterals. So you need to suppress the spaces at the end of the first line in:
Code:
    radiobutton .dialog.green -text  &quot;green&quot; \  <-spaces to suppress
          -variable colour -value green            -command { .frame1.label configure -bg $colour }
Here you dont have a \nSpace sequence but a \SpaceSpace sequence.

Your corrected script:
Code:
  frame .frame1 -background white
  label .frame1.label -text  &quot;I am a label&quot;   

  # create the button. disable it when it is clicked 
  button .frame1.button -text  &quot;Format label&quot;     -command { .frame1.button configure -state disabled; createDialog } 

  pack .frame1.label .frame1.button -side top 
  pack .frame1 -fill both -expand yes


  proc createDialog {}   {

    toplevel .dialog 

    radiobutton .dialog.green -text &quot;green&quot;       -variable colour -value green        -command { .frame1.label configure -bg $colour } 

    radiobutton .dialog.red -text  &quot;red&quot;       -variable colour -value red        -command { .frame1.label configure -bg $colour } 
    button .dialog.close -text  &quot;close window&quot;       -command { .frame1.button configure -state normal; destroy .dialog }
    set colour white 
    pack .dialog.green .dialog.red .dialog.close -side top
  }
HTH

ulis
 
Code:
Hi uils:
Thanks for you spend too much time for me, it was quit good 
lesson for me, thanks for you give me a very oppotunity to learn tcl/tk.

Now I got one question to ask you. I run your code, it works. But I followed your instruction to fix the bugs in my code untill the &quot;3/ Back to the endekalogue&quot;, then the compiler tell me there are an unknown option, but i could not find out why, and in my code, I put a mark with &quot;#&quot; where I put one space and two spaceses. would you tell me where I make mistake,please.

[code]
frame .frame1 -background white
label .frame1.label -text  &quot;I am a label&quot;   

# create the button. disable it when it is clicked
button .frame1.button -text  &quot;Format label&quot; -command { .frame1.button configure -state disabled; createDialog 
 }

pack .frame1.label .frame1.button -side top
pack .frame1 -fill both -expand yes


proc createDialog {} \ # wordspace{
  toplevel .dialog

  radiobutton .dialog.green -text &quot;green&quot; \  #wordspacs     -variable colour -value green  \   #wordSpaceSpace     -command { .frame1.label configure -bg $colour }

  radiobutton .dialog.red -text  &quot;red&quot; \ #wordspacs    -variable colour -value red  \   #wordSpaceSpace    -command { .frame1.label configure -bg $colour }
  button .dialog.close -text &quot;close window&quot; \  #wordspacs    -command { .frame1.button configure -state normal; destroy .dialog
     }
  set colour white
 pack .dialog.green .dialog.red .dialog.close -side top
}
 
Maybe I was not clear.

When you want to continue a line with the next line you need to finish the line with a back slash. This back slash must be the very last char of the line because it escapes the newline char:
Code:
  radiobutton .dialog.green -text &quot;green&quot;      -variable colour -value green       -command { .frame1.label configure -bg $colour }
So, in your code you need to delete all &quot;#wordspaces\&quot; you added.

One other thing: If you want to add a comment at a line, the comment must be the last command of the line. Yes a comment is a command. So you must separe it with a ; from the previous command on the line:
Code:
  mycommand and its args; # a comment
Further, a comment beeing a command (not the syntactical equivallent of a space) you cant add comments inside an other command.

HTH

ulis
 
That's not your problem, that's my understanding problem.

but after I delete the command off, it still reports error from the dialog box after I click on the &quot;format label&quot; of the label&quot;, it tell me there is a unknown option.

rgds
stewang
Code:
frame .frame1 -background white
label .frame1.label -text  &quot;I am a label&quot;   

# create the button. disable it when it is clicked
button .frame1.button -text  &quot;Format label&quot; -command { .frame1.button configure -state disabled; createDialog 
 }

pack .frame1.label .frame1.button -side top
pack .frame1 -fill both -expand yes


proc createDialog {} {
  toplevel .dialog

  radiobutton .dialog.green -text &quot;green&quot; \  
     -variable colour -value green       -command { .frame1.label configure -bg $colour }

  radiobutton .dialog.red -text  &quot;red&quot;     -variable colour -value red      -command { .frame1.label configure -bg $colour }
  button .dialog.close -text &quot;close window&quot;     -command { .frame1.button configure -state normal; destroy .dialog
     }
  set colour white
 pack .dialog.green .dialog.red .dialog.close -side top
}
 
Based upon my running the script you provided, I suspect that the problem is that you accidentally included an extra space or tab following one of the \s you used as a command continuation. For &quot;\&quot; to be interpreted as continuing the command to the next line, it must be the very last character on the line.

Think of it this way: The &quot;\&quot; character acts as an escape character. It tells the Tcl interpreter to treat the character following it in some special way. For line continuation, we are in essence &quot;escaping&quot; the newline character at the end of the line. The newline typically means &quot;end of command&quot; to the Tcl interpreter. But backslashing the newline tells Tcl that we're continuing the command to the next line.

So what happens if you accidentally put a space character after the &quot;\&quot; at the end of the line? You're telling the Tcl interpreter to &quot;escape&quot; the space. Therefore, Tcl treats the space as a literal character that is part of an argument, rather than being a whitespace character separating arguments. In other words, the following two examples are treated the same way by Tcl:

Code:
# There is a space following the \ on the next line
button .b -text &quot;Ok&quot; \ 
    -fg red

# There is no space following the \ on the next line
button .b -text &quot;Ok&quot; &quot; &quot;    -fg red

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Thanks for your time. you are right.I accidentally included with a tap following one of the \s.

rgds
stewang


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top