我应该如何处理下面gWidgets示例中的情况?
我想通过这个小图形用户界面实现的目标是拥有一个可以更新的子窗口。它运作良好。但是,如果用户通过单击“X”关闭“点击历史记录”窗口,则会出现错误:
Error: attempt to call 'GetBuffer' on invalid reference 'view'
看起来子窗口的引用/指针丢失了。我尝试了各种策略来解决问题。例如,使用tryCatch并重新创建窗口。这导致每次点击都打开一个新窗口。引用可以以某种方式保留吗?
更新:我在Windows 7系统上使用R 3.2.2和gWidgetsRGtk2工具包。
mainWindow <- function(){
require(gWidgets)
# This works well until the log window is closed.
# Next click gives this error:
# Error: attempt to call 'GetBuffer' on invalid reference 'view'
w_main <- gwindow(title="Main", visible=TRUE)
b_time <- gbutton("Show time!", container=w_main)
w_time <- gwindow(title="Click history", visible=FALSE)
t_time <- gtext("", container=w_time)
addHandlerChanged(b_time, handler = function(h, ...) {
# Log click.
insert(t_time, paste(date()))
# Show window.
visible(w_time) <- TRUE
} )
}
Update2:根据jverzani的建议实施似乎工作正常。事实上我尝试过类似的东西,但我想我忘记了<<-
mainWindow <- function(){
require(gWidgets)
# This works well also when the log window has been closed!
w_main <- gwindow(title="Main", visible=TRUE)
b_time <- gbutton("Show time!", container=w_main)
w_time <- gwindow(title="Click history", visible=FALSE)
t_time <- gtext("", container=w_time)
closed <- FALSE
addHandlerChanged(b_time, handler = function(h, ...) {
if(closed){
# Re-create window.
w_time <<- gwindow(title="Click history", visible=FALSE)
t_time <<- gtext("", container=w_time)
closed <<- FALSE
}
# Log click.
insert(t_time, paste(date()))
# Show window.
visible(w_time) <- TRUE
} )
addHandlerDestroy(w_time, handler = function(h, ...) {
# Subwindow closed!
closed <<- TRUE
} )
}
答案 0 :(得分:0)
以下是@jverzani提供的解决方案
mainWindow <- function(){
require(gWidgets)
# This works well also when the log window has been closed!
w_main <- gwindow(title="Main", visible=TRUE)
b_time <- gbutton("Show time!", container=w_main)
w_time <- gwindow(title="Click history", visible=FALSE)
t_time <- gtext("", container=w_time)
addHandlerChanged(b_time, handler = function(h, ...) {
if(!isExtant(w_time)){
# Re-create window.
w_time <<- gwindow(title="Click history", visible=FALSE)
t_time <<- gtext("", container=w_time)
}
# Log click.
insert(t_time, paste(date()))
# Show window.
visible(w_time) <- TRUE
} )
}