0

基本问题似乎是我无法从内部函数中检索小部件的状态。

我正在尝试使使用 gWidgets2RGtk2 开发的包与 gWidgets2tcltk 兼容。该软件包包括一个图形用户界面,实现了保存 gui 状态的可能性。该代码使用 RGtk2 可以按预期工作,但使用 tcltk 时会出现问题。我已经尝试过使用的 if 构造的各种变体。我发现我可以从 .loadState 中读取状态,但不能从 .saveState 中读取状态,这很奇怪。它们是从不同的地方调用的,那么问题是否与环境有关?我已经被困了一段时间,所以也许我对一个明显的解决方案视而不见,或者也许有更好的方法来完成我所追求的行为。下面的代码举例说明了这个问题。

我在 Windows 10 系统 gWidgets2tcltk_1.0-6 和 gWidgets2_1.0-7 上使用 R 版本 3.5.1。

# The code works as intended using RGtk2.
require(gWidgets2RGtk2)
options("guiToolkit"="RGtk2")

# The code does not work when using tcltk.
# require(gWidgets2tcltk)
# options("guiToolkit" = "tcltk")

saveStateExample <- function(env = parent.frame(), savegui = NULL) {

  # savegui = NULL, Default when started manually as GUI wrapped function.
  # savegui = TRUE, Passed from main GUI when started as part of full program.
  # savegui = FALSE, Passed from main GUI when started as part of full program.


  # Create windows.
  w <- gwindow(title = "Checkbox behaviour", visible = FALSE)

  # Runs when window is closed.
  addHandlerDestroy(w, handler = function(h, ...) {

    # Save GUI state.
    .saveState()
  })

  # Create container.
  g <- ggroup(container = w, expand = TRUE, horizontal = FALSE)

  # Add checkbox to control saving gui statesa.
  save_state_chk <- gcheckbox(text = "Save state", checked = FALSE, container = g)

  # Add a text widget.
  text_edt <- gedit(container = g)

  # Add buttons to manually trigger the functions.
  load_btn <- gbutton(text = "Run .loadState", container = g)
  save_btn <- gbutton(text = "Run .saveState", container = g)

  addHandlerChanged(load_btn, handler = function(h, ...) {

    .loadState()

  })

  addHandlerChanged(save_btn, handler = function(h, ...) {

    .saveState()

  })

  # Internal function ---------------------------------------------------------

  .loadState <- function() {
    message(".loadState")
    message("save_state_chk was ", svalue(save_state_chk))
    message("savegui was ", savegui)

    # First check if save argument was passed.
    if (!is.null(savegui)) {
      # Update widget with passed value.
      svalue(save_state_chk) <- savegui
      message("save_state_chk set to ", savegui)
      message("save_state_chk is ", svalue(save_state_chk))
    } else {
      # Look for previously saved flag.
      if (exists(".package_savegui", envir = env, inherits = FALSE)) {
        svalue(save_state_chk) <- get(".package_savegui", envir = env)
        message(".package_savegui loaded")
      }
    }

    message("LOAD SAVED STATE")

    # Then load settings if true.
    if (svalue(save_state_chk)) {
      if (exists(".package_text", envir = env, inherits = FALSE)) {
        svalue(text_edt) <- get(".package_text", envir = env)
      }

      message("GUI saved state loaded")
    } else {
      message("GUI default state loaded")
    }
  }

  # Internal function ---------------------------------------------------------

  .saveState <- function() {
    message(".saveState")
    message("save_state_chk was ", svalue(save_state_chk))
    message("savegui was ", savegui)

    # First check status of save flag.
    if (is.null(svalue(save_state_chk))) {
      message("save_state_chk=NULL")
    } else {
      message("SAVE STATE")
      # Then save settings if true.
      if (svalue(save_state_chk)) {
        assign(x = ".package_savegui", value = svalue(save_state_chk), envir = env)
        assign(x = ".package_text", value = svalue(text_edt), envir = env)

        message("GUI state saved")
      } else { # or remove all saved values if false.

        if (exists(".package_savegui", envir = env, inherits = FALSE)) {
          remove(".package_savegui", envir = env)
        }
        if (exists(".package_text", envir = env, inherits = FALSE)) {
          remove(".package_text", envir = env)
        }

        message("GUI state cleared")
      }
    }
  }

  # Run internal function to load state before showing window.
  .loadState()
  visible(w) <- TRUE
}

# Open gui.
saveStateExample()

EDIT1:我在下面做了一个稍微小一点的例子,它可以与你描述的两个工具包一起使用。目的是确保内部功能按预期工作,他们确实做到了。

为了进一步追查问题,我编辑了上面的第一个代码示例,以包含手动触发内部功能的按钮。按钮工作,状态可读取并由message功能打印。但是,当触发销毁处理程序(使用 tcltk 工具包)时,仍然不会读取状态。似乎使用 tcltk 工具包过早地破坏了某些东西,而不是使用 RGtk2 工具包。有任何想法吗?

# The code works as intended using RGtk2 and tcltk.
# require(gWidgets2RGtk2)
# options("guiToolkit"="RGtk2")

 require(gWidgets2tcltk)
 options("guiToolkit" = "tcltk")

saveStateExample <- function() {

  # Create windows.
  w <- gwindow(title = "Checkbox behaviour", visible = FALSE)

  # Runs when window is closed.
  addHandlerDestroy(w, handler = function(h, ...) {

    message("Window destroyed")
    message("save_state_chk is ", svalue(save_state_chk))
    message("text_edt is ", svalue(text_edt))

  })

  # Create container.
  g <- ggroup(container = w, expand = TRUE, horizontal = FALSE)

  # Add checkbox to control saving gui statesa.
  save_state_chk <- gcheckbox(text = "Save state", checked = FALSE, container = g)

  # Add a text widget.
  text_edt <- gedit(container = g)

  # Add buttons.
  check_btn <- gbutton(text = "Check", container = g)
  uncheck_btn <- gbutton(text = "UnCheck", container = g)

  addHandlerChanged(check_btn, handler = function(h, ...) {

    .setCheckTrue()

  })

  addHandlerChanged(uncheck_btn, handler = function(h, ...) {

    .setCheckFalse()

  })

  # Internal function ---------------------------------------------------------

  .setCheckTrue <- function() {

    message(".setCheckTrue")
    message("save_state_chk was ", svalue(save_state_chk))

    svalue(save_state_chk) <- TRUE
    message("save_state_chk is ", svalue(save_state_chk))
    message("text_edt is ", svalue(text_edt))

  }

  # Internal function ---------------------------------------------------------

  .setCheckFalse <- function() {

    message(".setCheckFalse")
    message("save_state_chk was ", svalue(save_state_chk))

    svalue(save_state_chk) <- FALSE
    message("save_state_chk is ", svalue(save_state_chk))
    message("text_edt is ", svalue(text_edt))

  }

  # Show window.
  visible(w) <- TRUE

}

# Open gui.
saveStateExample()

EDIT2:感谢@jverzani 的提示,我已按照您的建议尝试了 addHandlerUnrealize 。这解决了不读取 tcltk 任何值的问题。然而,我花了一些时间来整理并让它与两个工具包一起工作——窗口无法关闭。我在文档中没有找到太多内容,但通过反复试验,似乎 RGtk2 和 tcltk 实施了不同的物流。要继续销毁事件 RGtk2 需要FALSE,而 tcltk 需要TRUE。下面是第一个代码示例的固定版本:

# The code now works as intended using both RGtk2 and tcltk!
# require(gWidgets2RGtk2)
# options("guiToolkit"="RGtk2")
require(gWidgets2tcltk)
options("guiToolkit" = "tcltk")

saveStateExample <- function(env = parent.frame(), savegui = NULL) {

  # savegui = NULL, Default when started manually as GUI wrapped function.
  # savegui = TRUE, Passed from main GUI when started as part of full program.
  # savegui = FALSE, Passed from main GUI when started as part of full program.


  # Create windows.
  w <- gwindow(title = "Checkbox behaviour", visible = FALSE)

  # Runs when window is closed.
  addHandlerUnrealize(w, handler = function(h, ...) {

    # Save GUI state.
    .saveState()

    message("UNREALIZE!")

    # Check which toolkit we are using.
    if (gtoolkit() == "tcltk") {
      message("tcltk, returned TRUE!")
      return(TRUE) # Destroys window under tcltk, but not RGtk2.
    } else {
      message("RGtk2, returned FALSE!")
      return(FALSE) # Destroys window under RGtk2, but not with tcltk.
    }
  })

  # Runs when window is closed.
  addHandlerDestroy(w, handler = function(h, ...) {
    message("DESTROY!")
    # addHandlerDestroy does not care of return type for either RGtk2 or tcltk?
  })

  # Create container.
  g <- ggroup(container = w, expand = TRUE, horizontal = FALSE)

  # Add checkbox to control saving gui statesa.
  save_state_chk <- gcheckbox(text = "Save state", checked = FALSE, container = g)

  # Add a text widget.
  text_edt <- gedit(container = g)

  # Add buttons to manually trigger the functions.
  load_btn <- gbutton(text = "Run .loadState", container = g)
  save_btn <- gbutton(text = "Run .saveState", container = g)

  addHandlerChanged(load_btn, handler = function(h, ...) {
    .loadState()
  })

  addHandlerChanged(save_btn, handler = function(h, ...) {
    .saveState()
  })

  # Internal function ---------------------------------------------------------

  .loadState <- function() {
    message(".loadState")
    message("save_state_chk was ", svalue(save_state_chk))
    message("savegui was ", savegui)

    # First check if save argument was passed.
    if (!is.null(savegui)) {
      # Update widget with passed value.
      svalue(save_state_chk) <- savegui
      message("save_state_chk set to ", savegui)
      message("save_state_chk is ", svalue(save_state_chk))
    } else {
      # Look for previously saved flag.
      if (exists(".package_savegui", envir = env, inherits = FALSE)) {
        svalue(save_state_chk) <- get(".package_savegui", envir = env)
        message(".package_savegui loaded")
      }
    }

    message("LOAD SAVED STATE")

    # Then load settings if true.
    if (svalue(save_state_chk)) {
      if (exists(".package_text", envir = env, inherits = FALSE)) {
        svalue(text_edt) <- get(".package_text", envir = env)
      }

      message("GUI saved state loaded")
    } else {
      message("GUI default state loaded")
    }
  }

  # Internal function ---------------------------------------------------------

  .saveState <- function() {
    message(".saveState")
    message("save_state_chk was ", svalue(save_state_chk))
    message("savegui was ", savegui)

    # First check status of save flag.
    if (is.null(svalue(save_state_chk))) {
      message("save_state_chk=NULL")
    } else {
      message("SAVE STATE")
      # Then save settings if true.
      if (svalue(save_state_chk)) {
        assign(x = ".package_savegui", value = svalue(save_state_chk), envir = env)
        assign(x = ".package_text", value = svalue(text_edt), envir = env)

        message("GUI state saved")
      } else { # or remove all saved values if false.

        if (exists(".package_savegui", envir = env, inherits = FALSE)) {
          remove(".package_savegui", envir = env)
        }
        if (exists(".package_text", envir = env, inherits = FALSE)) {
          remove(".package_text", envir = env)
        }

        message("GUI state cleared")
      }
    }
  }

  # Run internal function to load state before showing window.
  .loadState()
  visible(w) <- TRUE
}

# Open gui.
saveStateExample()
4

1 回答 1

0

问题的原因addHandlerDestroy是不适合保存小部件状态。它可能像 RGtk2 一样工作,但不能保证此时小部件可以访问。addHandlerUnrealize正如@jverzani 在评论中指出的那样,解决方案是改用:

您应该在这里尝试 addHandlerUnrealize (尽管我不知道这是否适用于 RGtk2 并且无法测试)。只要您的处理程序不返回 FALSE 但将在销毁事件之前执行,这将调用销毁,因此您的小部件仍然可以读取。– jverzani 1 月 24 日 16:38

有趣的是,示例代码揭示了发出窗口销毁信号的不同实现(有关解决方法,请参阅原始帖子)。请注意,@jverzani 考虑推动修复,这将改变这一点。

于 2019-01-29T08:24:04.907 回答