2

In the following code I would like to reset the entry fields, but once I hit the Submit button, I can not reset them any more. If I do Reset before the Submit button then everything is ok. What am I missing?

library(tcltk);

doneInput <- tclVar(0)

tkwindow <- tktoplevel();

l.directory <- tklabel (tkwindow, text = "directory path: ");
l.folder1 <- tklabel (tkwindow, text = "folder name: ");
l.folderO <- tklabel (tkwindow, text = "folder name: ");

o.directory <- tclVar("Enter directory path ...");
o.folder1 <- tclVar("Enter folder name ...");
o.folderO <- tclVar("Enter folder name ...");

e.directory <- tkentry (tkwindow, textvariable = o.directory);
e.folder1 <- tkentry (tkwindow, textvariable = o.folder1);
e.folderO <- tkentry (tkwindow, textvariable = o.folderO);

tkgrid (l.directory, e.directory);
tkgrid (l.folder1, e.folder1);
tkgrid (l.folderO, e.folderO);

tkgrid.configure (l.directory, l.folder1, l.folderO, sticky="e");
tkgrid.configure (e.directory, e.folder1, e.folderO, sticky="w");

OnReset <- function()
{
    tclvalue (o.directory) <<- "path ...";
    tclvalue (o.folder1) <<- "name ...";
    tclvalue (o.folderO) <<- "name ...";

    tkconfigure (b.submit, state = "normal");
}
b.reset <- tkbutton (tkwindow, text = "Reset", command = OnReset)

OnSubmit <- function()
{
    o.directory <<- tclvalue (o.directory);
    o.folder1 <<- tclvalue (o.folder1);
    o.folderO <<- tclvalue (o.folderO);

    print (o.directory);
    print (o.folder1);
    print (o.folderO);

    if (nchar (o.directory) > 0 && 
        nchar (o.Folder1) > 0 &&
        nchar (o.FolderO) > 0)
        tclvalue (doneInput) <- 1;
}
b.submit <- tkbutton (tkwindow, text = "Submit", command = OnSubmit);
tkgrid (b.submit, b.reset, sticky="e", padx=10, pady=10);

tkwait.variable (doneInput)

print (paste ("directory: ", o.directory));
print (paste ("folder1: ", o.folder1));
print (paste ("folderO: ", o.folderO));
gaitat
  • 12,449
  • 4
  • 52
  • 76
  • Why are you using `tkwait.variable`? Also (just as a style comment), you don't need semicolons at the end of R code lines. – Thomas Jul 29 '14 at 19:15
  • It is not in the code that I posted but I want these tkentries to be valid before the code continues on. As for the semicolons ... I have 20 years in C/C++. I am used to them. It is almost automatic. – gaitat Jul 29 '14 at 19:18
  • sorry, fixed it. Thats what I get for having two version of code. And obviously the if-statement that will change the value of `doneInput` is only there to do some kind of validation and is always true with the given data strings. – gaitat Jul 29 '14 at 21:51

1 Answers1

0

The issue here is the <<- operator used inside the functions (e.g., o.directory <<- tclvalue (o.directory);, etc.). Replacing them with <- solves your problem.

Leaving everything else the same, if you change your OnReset and OnSubmit functions to the following, you should get your intended result:

OnReset <- function()
{
    tclvalue (o.directory) <- "path ...";
    tclvalue (o.folder1) <- "name ...";
    tclvalue (o.folderO) <- "name ...";

    tkconfigure (b.submit, state = "normal");
}

OnSubmit <- function()
{
    a <- tclvalue (o.directory);
    b <- tclvalue (o.folder1);
    c <- tclvalue (o.folderO);

    print (a);
    print (b);
    print (c);

    if (nchar (a) > 0 && 
        nchar (b) > 0 &&
        nchar (c) > 0)
        tclvalue (doneInput) <- 1;
}
Thomas
  • 43,637
  • 12
  • 109
  • 140
  • thank you but can you briefly explain the difference bettwen `<-` and `<<-` – gaitat Jul 29 '14 at 19:28
  • @gaitat There are some answers on the distinction here: http://stackoverflow.com/questions/2628621/how-do-you-use-scoping-assignment-in-r – Thomas Jul 29 '14 at 19:39
  • sorry but when I use `<-` the reset works but my values do not persist outside of my `OnSubmit` function. I need both the reset to work and the print (at the end of the code) to work. – gaitat Jul 29 '14 at 21:18
  • 1
    @gaitat Okay, there are actually several issues here. (1) Your `tkwait.variable()` call is waiting a variable that you never change, so you'll never break out of the tcl event loop. (2) You're initializing tcl variables and then later (in `onSubmit`) trying to overwrite them as simple character strings (every time you `<<-`). Because of that, the next time tcl tries to update the variables it can't because they're not tcl variables, they're character strings. So, now I'm actually not clear on what you're trying to achieve. – Thomas Jul 29 '14 at 21:33
  • Updated the code above to show that I am waiting until the input is validated. So what I would like is `onSubmit` to get the values of the tkentries and continue my processing. But I would also like to be able to `reset` the tkentries to their original values. The code above does not accomplish both these tasks. Just one. – gaitat Jul 29 '14 at 21:42