我的脚本需要等待用户输入才能继续处理。在使用R gWidgets包实现GUI之后,我发现gbasicdialog小部件比gwindow工作得更好,因为它是模态的。
gwindow小部件具有有用的最小化和最大化按钮。 gbasicdialog小部件没有这些按钮,我经常在GUI中显示大型gtables,这意味着用户必须反复点击并拖动才能展开窗口以查看足够的表格。
到目前为止,我按如下方式调整gtable小部件的大小:
library(gWidget)
library(gWidgetsRGtk2)
library(RGtk2)
options(guiToolkit = "RGtk2")
mywindow <- gbasicdialog("Display table", do.buttons = FALSE)
# there can be multiple ggroups to one table, which all occupy the gbasicdialog
mygroup <- ggroup(container = mywindow, horizontal = FALSE)
mytable <- gtable(a_data_frame, container = mygroup, expand = TRUE, fill = TRUE)
size(mytable) <- c(500, 500) # or any other large enough dimension
exitbutton <- gbutton("Done", container = mywindow,
handler = function(h, ...) {
#some steps
dispose(mywindow)
})
visible(mywindow, TRUE)
如何为交互式/模态窗口显示最大化/最小化按钮,或尽可能多地显示大型小部件?
答案 0 :(得分:2)
在查看Gtk工具包之后,某些方法与GtkWindow和GtkDialog有关。 Maximize方法(自动最大化窗口小部件)适用于两者,但SetDecorated方法(显示包含最小化,最大化,关闭按钮的上方栏)仅适用于GtkWindow。
我们可以使用gwindow小部件获取调整大小按钮,同时添加重复循环以暂停其余脚本的执行,从而保持模态/交互行为。在jverzani的评论中,自动最大化小部件的步骤也可以添加到此gwindow中。
currentenv <- environment() # get current environment
mybool <- FALSE # remains FALSE until the gwindow is closed
mywindow <- gwindow("Display table")
# Implement auto-maximized gwindow if needed
getToolkitWidget(mywindow)$maximize()
# there can be multiple ggroups to one table, which all occupy the gwindow
mygroup <- ggroup(container = mywindow, horizontal = FALSE)
mytable <- gtable(a_data_frame, container = mygroup, expand = TRUE, fill = TRUE)
# Upon exit, assign TRUE to variable denoting gwindow is closed
exitbutton <- gbutton("Done", container = mygroup,
handler = {
# (some steps)
# possibly returning svalue(mytable)
# remove ggroup to prevent error
delete(mywindow, mygroup)
# close window and confirm closure
dispose(mywindow)
assign("mybool", TRUE, envir = currentenv)
})
# Executes repeat loop until mywindow is closed
repeat{
if(mybool){
break
}
}