可以在R中的同一个函数或窗口中使用tkgrid()和tkpack(),还是可以替换tkframe()?

时间:2015-09-22 23:59:10

标签: r tcl tk

我正在使用R中的tcl / tk创建用户界面。我需要在单个顶级窗口中包含几个按钮和其他小部件,我需要将一些小部件组合到顶级窗口中的容器中。我一直在使用tkframe()作为容器,据我所知,需要tkpack()来放置小部件。但是我使用tkgrid()将框架和其他小部件放在顶层窗口中。我怀疑当我尝试运行接口函数时,这会导致我遇到的不一致。可以一起使用tkpack()和tkgrid()吗?如果没有,是否有其他类型的容器可以使用而不是tkframe(),它可以更好地与tkgrid()一起使用?

以下是一些简化代码作为仅使用tkpack()的示例:

 require(tcltk)   
 tclRequire("BWidget")



interface1 <- function(){

  ww <- tktoplevel()

  buttons.f <- tkframe(ww)

   getdata.b <- tkbutton(buttons.f, text = "Import Data", command = function() {
    filepath <<- file.choose()
    })
   viewdata.b <- tkbutton(buttons.f, text = "Print Data", command = function() {
    print(read.csv(filepath))
    })
   tkpack(getdata.b, fill = "both")
   tkpack(viewdata.b, fill = "both")

  tree.f = tkframe(ww)

   treeWidget = tkwidget(tree.f,"Tree", width = 30, height = 6)

   tkinsert(treeWidget,"end","root","Species1",text="Elk")
   tkinsert(treeWidget,"end","root","Species2",text="Deer")

   tkpack(treeWidget,fill='both',expand=1)

   getvars  <- function() { 
    selection <<- tclvalue(tcl(treeWidget,"selection","get")) 
    print(selection)
    }

  run.b <- tkbutton(ww, text = "Run", command = getvars)

  close.b <- tkbutton(ww, text = "Close", command = function(){
    tkdestroy(ww)
    filepath <<- NULL
    })

  tkpack(buttons.f)
  tkpack(tklabel(ww,text="Select One:"))
  tkpack(tree.f)
  tkpack(run.b, fill = "both")
  tkpack(close.b, fill = "both")
}
interface1()

但是使用tkgrid()时,它并不总能正确显示:

interface2 <- function(){

  ww <- tktoplevel()

  buttons.f <- tkframe(ww)

   getdata.b <- tkbutton(buttons.f, text = "Import Data", command = function() {
    filepath <<- file.choose()
    })
   viewdata.b <- tkbutton(buttons.f, text = "Print Data", command = function() {
    print(read.csv(filepath))
    })
   tkpack(getdata.b, fill = "both")
   tkpack(viewdata.b, fill = "both")

  tree.f = tkframe(ww)

   treeWidget = tkwidget(tree.f,"Tree", width = 30, height = 6)

   tkinsert(treeWidget,"end","root","Species1",text="Elk")
   tkinsert(treeWidget,"end","root","Species2",text="Deer")

   tkpack(treeWidget,fill='both',expand=1)

   getvars  <- function() { 
    selection <<- tclvalue(tcl(treeWidget,"selection","get")) 
    print(selection)
    }

  run.b <- tkbutton(ww, text = "Run", command = getvars)

  close.b <- tkbutton(ww, text = "Close", command = function(){
    tkdestroy(ww)
    filepath <<- NULL
    })

  tkgrid(buttons.f, pady = 10, columnspan = 2)
  tkgrid(tklabel(ww,text="Select One:"), columnspan = 2)
  tkgrid(tree.f, columnspan = 2)
  tkgrid(run.b, close.b, padx = 20, pady = 10)
}
interface2()

由于我更喜欢​​使用tkgrid,任何人都可以确定tkgrid / tkpack的组合是否导致错误,还是有人可以建议替代tkframe?谢谢你的帮助!

(我确信有一个很好的包来运行这个任务,但是我正在为一个项目创建一个接口,我被指示不要使用任何没有基础R的软件包) / p>

1 个答案:

答案 0 :(得分:1)

严格地说,如果关闭(至少)其中一个的几何传播,则可以在同一帧中同时使用打包器和网格器。如果你不这样做,他们会争夺父框架的大小。但你可能不希望首先这样做;这不太可能是你需要的小部件布局,因为小部件会堆叠在一起并且不会重叠。

在框架中使用封隔器,在其子框架中使用网格,或者反过来,这是完全合理的。这几乎可以肯定你想做什么。