How can I add a context menu to a gframe
in gWidgets2
? I tried the following construct, but it seems that I cannot attach a addPopupMenu
to a gframe
:
如何向gWidgets2中的gframe添加上下文菜单?我尝试了下面的构造,但是似乎我不能在gframe上附加一个addPopupMenu:
require(gWidgets2)
w <- gwindow("gformlayout", visible=T)
f <- gframe("frame", horizontal=FALSE, container=w)
l <- glabel("Lorem ipsum dolor sit amet, \nconsectetur adipiscing elit.", container=f)
b <- gbutton("change name", container=f, handler=function(h,...) {
names(f) <- "new name"
})
lst <- list(gaction('world', handler=function(h,...) svalue(world) <- "world"),
gaction('continent', handler=function(h,...) svalue(world) <- "continent"),
gaction('country', handler=function(h,...) svalue(world) <- "country"),
gaction('state', handler=function(h,...) svalue(world) <- "state"))
add3rdmousePopupMenu(f, lst)
#addPopupMenu(f, lst)
add3rdmousePopupMenu(b, lst)
The context-menu is attached fine to the button, but NOT to the gframe
. So how can I add a context menu that would pop-up when right-clicking the gframe
label?
上下文菜单可以很好地附加到按钮上,但不能附加到gframe上。那么,当右键单击gframe标签时,如何添加上下文菜单?
UPDATE
As per the answers, I tried the code below:
根据答案更新,我尝试了下面的代码:
require(gWidgets2)
f <- gframe("", cont=gwindow())
l <- glabel("label") # no cont argument
add3rdmousePopupMenu(l, list(a=gaction("Hi")))
f$block$setLabelWidget(l$block) # the voodoo
But when I right-click
on the label
, I only get the standard Gtk context menu for selecting text:
但是当我右键点击标签时,我只会得到标准的Gtk上下文菜单来选择文本:
Using Ubuntu 14.04 with Gtk+ 2.24.23. My sessionInfo()
:
使用Ubuntu 14.04和Gtk+ 2.24.23。我sessionInfo():
> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] splines grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] reshape2_1.4 gWidgets2RGtk2_1.0-3 memoise_0.2.1 Hmisc_3.14-4 Formula_1.1-1
[6] survival_2.37-7 lattice_0.20-29 RGtk2_2.20.29 gWidgets2_1.0-6 digest_0.6.4
loaded via a namespace (and not attached):
[1] cluster_1.14.4 latticeExtra_0.6-26 plyr_1.8.1 RColorBrewer_1.0-5 Rcpp_0.11.2
[6] stringr_0.6.2 tools_3.0.2
2 个解决方案
#1
1
@landroni I didn't check, but would be surprised if popup menus could be added to gframe labels directly. But with Gtk things can be worked around. Here is how you can put a popup menu in the label position:
@landroni我没有检查,但是如果弹出菜单可以直接添加到gframe标签中,我会很惊讶。但是有了Gtk,事情就可以解决了。以下是在标签位置放置弹出菜单的方法:
f <- gframe("", cont=gwindow())
l <- glabel("label") # no cont argument
add3rdmousePopupMenu(l, list(a=gaction("Hi")))
f$block$setLabelWidget(l$block) # the voodoo
l$widget$setSelectable(FALSE) # may not be needed
As for whether this is intuitive to the users, I'll let you decide....
至于这是直观的用户,是否我会让你决定....
#2
1
A popup menu “pops” up a menu after a mouse click, typically a right mouse click. Implemented in gWidgets are the functions 1add3rdmousepopupmenu(the one you want) and
addpopupmenu` for a popup on any click. The menu is specified using the syntax for gmenu.
一个弹出菜单在鼠标点击后弹出,通常是鼠标右键。在gWidgets中实现的是函数1add3rdmousepopupmenu(你想要的)和addpopupmenu“用于任何点击的弹出”。使用gmenu的语法指定菜单。
A simple example would be something like:
一个简单的例子是:
> w <- gwindow("Click on button to change")
> g <- ggroup(cont = w) # abbreviate container
> glabel("Hello ", cont=g)
guiWidget of type: gLabelRGtk for toolkit: guiWidgetsToolkitRGtk2
> world <- gbutton("world", cont=g)
> lst <- list()
> lst$world$handler <- function(h,...) svalue(world) <- "world"
> lst$continent$handler <- function(h,...) svalue(world) <- "continent"
> lst$country$handler <- function(h,...) svalue(world) <- "country"
> lst$state$handler <- function(h,...) svalue(world) <- "state"
> add3rdmousepopupmenu(world, lst)
I had a chance to test this and it works for me with gWidgets
, gWidgets2
, and gWidgetsRGtk2
using 64-bit R 3.1.1 on Windows. You can find documentation for the add3rdmousepopupmenu
handler
我有机会对它进行测试,它适用于在Windows上使用64位r3.1.1的gwidget、gWidgetsRGtk2和gWidgetsRGtk2。您可以找到add3rdmousepopupmenu处理程序的文档
- in the
gWidgets2
manual here or - 在gWidgets2手册中
- by typing
??add3rdmousepopupmenu
withgWidgets2
loaded, - 通过输入? ?add3rdmousepopupmenu gWidgets2加载,
- in the recently updated document by the author John Verzani giving examples for the gWidgets API, which applies not only to
gWdigets2
, but also hisgWidgets
,gWidgetsQt
,gWidgetsRGtk2
, andgWidgetsrJava
packages which call the gWdigets API - 在作者John Verzani最近更新的文档中,给出了gWidgets API的示例,它不仅适用于gwts2,还适用于gwidget、gWidgetsQt、gWidgetsRGtk2和gWidgetsrJava包,它们调用gWdigets API
- and in the
gWidgets2
vignette on CRAN - 在gWidgets2小片段中
#1
1
@landroni I didn't check, but would be surprised if popup menus could be added to gframe labels directly. But with Gtk things can be worked around. Here is how you can put a popup menu in the label position:
@landroni我没有检查,但是如果弹出菜单可以直接添加到gframe标签中,我会很惊讶。但是有了Gtk,事情就可以解决了。以下是在标签位置放置弹出菜单的方法:
f <- gframe("", cont=gwindow())
l <- glabel("label") # no cont argument
add3rdmousePopupMenu(l, list(a=gaction("Hi")))
f$block$setLabelWidget(l$block) # the voodoo
l$widget$setSelectable(FALSE) # may not be needed
As for whether this is intuitive to the users, I'll let you decide....
至于这是直观的用户,是否我会让你决定....
#2
1
A popup menu “pops” up a menu after a mouse click, typically a right mouse click. Implemented in gWidgets are the functions 1add3rdmousepopupmenu(the one you want) and
addpopupmenu` for a popup on any click. The menu is specified using the syntax for gmenu.
一个弹出菜单在鼠标点击后弹出,通常是鼠标右键。在gWidgets中实现的是函数1add3rdmousepopupmenu(你想要的)和addpopupmenu“用于任何点击的弹出”。使用gmenu的语法指定菜单。
A simple example would be something like:
一个简单的例子是:
> w <- gwindow("Click on button to change")
> g <- ggroup(cont = w) # abbreviate container
> glabel("Hello ", cont=g)
guiWidget of type: gLabelRGtk for toolkit: guiWidgetsToolkitRGtk2
> world <- gbutton("world", cont=g)
> lst <- list()
> lst$world$handler <- function(h,...) svalue(world) <- "world"
> lst$continent$handler <- function(h,...) svalue(world) <- "continent"
> lst$country$handler <- function(h,...) svalue(world) <- "country"
> lst$state$handler <- function(h,...) svalue(world) <- "state"
> add3rdmousepopupmenu(world, lst)
I had a chance to test this and it works for me with gWidgets
, gWidgets2
, and gWidgetsRGtk2
using 64-bit R 3.1.1 on Windows. You can find documentation for the add3rdmousepopupmenu
handler
我有机会对它进行测试,它适用于在Windows上使用64位r3.1.1的gwidget、gWidgetsRGtk2和gWidgetsRGtk2。您可以找到add3rdmousepopupmenu处理程序的文档
- in the
gWidgets2
manual here or - 在gWidgets2手册中
- by typing
??add3rdmousepopupmenu
withgWidgets2
loaded, - 通过输入? ?add3rdmousepopupmenu gWidgets2加载,
- in the recently updated document by the author John Verzani giving examples for the gWidgets API, which applies not only to
gWdigets2
, but also hisgWidgets
,gWidgetsQt
,gWidgetsRGtk2
, andgWidgetsrJava
packages which call the gWdigets API - 在作者John Verzani最近更新的文档中,给出了gWidgets API的示例,它不仅适用于gwts2,还适用于gwidget、gWidgetsQt、gWidgetsRGtk2和gWidgetsrJava包,它们调用gWdigets API
- and in the
gWidgets2
vignette on CRAN - 在gWidgets2小片段中