R Tcl / Tk:在OnSubmit之后OnReset无法正常工作

时间:2014-07-29 19:02:22

标签: r tcl tk

在下面的代码中,我想重置输入字段,但是一旦我点击“提交”按钮,我就无法重置它们了。如果我在提交按钮之前重置,那么一切正常。我错过了什么?

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));

1 个答案:

答案 0 :(得分:0)

此处的问题是函数内部使用的<<-运算符(例如o.directory <<- tclvalue (o.directory);等)。用<-替换它们可以解决您的问题。

如果您将OnResetOnSubmit功能更改为以下内容,请保留其他所有内容,您应该得到预期的结果:

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;
}