【问题标题】:How to change background/foreground color of a `gedit()` or `gcombobox()` input box?如何更改`gedit()`或`gcombobox()`输入框的背景/前景颜色?
【发布时间】:2014-07-19 08:16:59
【问题描述】:

考虑以下输入框:

w <- gwindow()
g <- ggroup(cont=w, horizontal=F)
ed <- gedit(cont=g)
cbb <- gcombobox(letters, editable=T, use_completion=T, cont=g)

gedit()/gcombobox()输入框包含字符串asdf时,如何使背景为红色,前景(文本本身)为白色?

【问题讨论】:

  • 如果e 是条目小部件,则类似这样的东西应该可以工作:e$widget$modifyBase(GtkStateType["normal"], "red")。可以通过传入NULL来清除修改。
  • 前景的颜色(即文本本身)怎么样。可以改吗?
  • 有这些方法:modifyBasemodifyTextmodifyBgmodifyFg(可能还有其他方法)来调整这些东西。我认为Fg 是您想要的。
  • 那么gcombobox() 呢?我尝试了cbb &lt;- gcombobox(letters, editable=T, use_completion=T, cont=ggroup(cont=w)) 然后cbb$widget$modifyBase(GtkStateType["normal"], "red"),但没有任何反应。
  • 我认为您需要在组合框中获取条目小部件才能按上述方式进行修改。我有这个彻底的技巧:cb$widget$getChildren()[[1]] 来获得这个可编辑的组合框。也许有一种方法可以正确地做到这一点。

标签: r colors gwidgets background-foreground


【解决方案1】:

按照 cmets 中的建议,以下内容可以完成工作,您可以将 red 替换为 #FF6666(淡红色)或任何颜色:

addHandlerKeystroke(ed, function(h,...){
    if(svalue(ed)=="asdf"){ 
        ed$widget$modifyBase(GtkStateType["normal"], "red") 
        ed$widget$modifyText(GtkStateType["normal"], "white") 
    } else {
        ed$widget$modifyBase(GtkStateType["normal"], NULL)
        ed$widget$modifyText(GtkStateType["normal"], NULL) 
    }
})

gcombobox() 略有不同:

addHandlerChanged(cbb, function(h,...){
    if(svalue(cbb)=="asdf"){ 
        cbb$widget$getChildren()[[1]]$modifyBase(GtkStateType["normal"], "red")
        cbb$widget$getChildren()[[1]]$modifyText(GtkStateType["normal"], "white")
    } else {
        cbb$widget$getChildren()[[1]]$modifyBase(GtkStateType["normal"], NULL)
        #cbb$widget$getChildren()[[1]]$modifyText(GtkStateType["normal"], NULL)
    }
})

【讨论】:

    猜你喜欢
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2020-05-19
    • 2013-05-20
    相关资源
    最近更新 更多