首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可调整大小的Tk窗口已正确完成

可调整大小的Tk窗口已正确完成
EN

Stack Overflow用户
提问于 2011-06-04 12:29:22
回答 3查看 1.9K关注 0票数 2

我组装Tk窗口的方式有问题(使用tcltk2和tcltk2,在Win XP下)

代码语言:javascript
复制
library(tcltk)
library(tcltk2)

expandTk <- function() {
  root  <- tktoplevel()
  # textbox with scroll bars
  textbox <- tk2frame(root)
  scr <- tkscrollbar(textbox, repeatinterval=5, command=function(...) tkyview(txt,...))
  txt <- tktext(textbox, bg="white", font="courier", wrap="word", yscrollcommand=function(...)tkset(scr,...))
  tkpack(txt, side="left", fill="both", expand=TRUE)
  tkpack(scr, side="right", fill="y")
  tkmark.set(txt,"insert","0.0")
  tkpack(textbox, fill="both", expand=TRUE)
  # status bar and size grip
  statusText <- tclVar("")
  f <- tk2frame(root, relief="sunken")
  l <- tk2label(f, textvariable=statusText)
  tkpack(l, side="left", pady=2, padx=5, expand=0, fill="x")
  tkpack(f, side="left", expand=1, fill="x", anchor="s")
  sg <- ttksizegrip(root)
  tkpack(sg, side="left", expand=0, anchor="se")
}

这个窗口看起来很好,但是我一调整它的大小,滚动条和状态栏就消失了。我很确定这是一个用户错误,我已经看到其他Tk应用程序可以正确调整大小,但我不知道我应该使用哪个选项…

任何提示都很感谢,卡斯腾

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-04 16:35:07

这是Tk pack几何管理器的标准行为。以下是pack手册页的相关部分:

如果腔变得太小而不能满足从机的需求,那么无论腔中剩余的空间是什么,从机都将被给予。如果空洞缩小到零大小,那么装箱单上的所有剩余从属窗口将从屏幕上取消映射,直到主窗口变得足够大,可以再次容纳它们。

因此,如果将整个窗口缩小到小于文本小部件所需的空间,则不会为其他小部件留出任何空间,并且这些小部件将无法映射。

最好的解决方案是使用grid几何图形管理器,而不是pack。总的来说,我发现gridpack更容易使用,也更有能力,尽管您的使用里程可能会有所不同。特别是,它消除了对许多多余的框架小部件的需要,这可以大大简化您的代码。

我认为你可以使用这样的东西:

代码语言:javascript
复制
expandTk <- function() {
  root  <- tktoplevel()
  # textbox with scroll bars
  textbox <- tk2frame(root)
  txt <- tktext(textbox, bg="white", font="courier", wrap="word", yscrollcommand=function(...)tkset(scr,...))
  scr <- tkscrollbar(textbox, repeatinterval=5, command=function(...) tkyview(txt,...))
  tkmark.set(txt,"insert","0.0")

  # Set up the geometry for the stuff inside the "textbox" frame.

  # The text and scrollbar widgets live on the same row.
  tkgrid(txt, scr)

  # The text widget should stick to all four sides of its parcel.
  tkgrid.configure(txt, sticky="nsew")

  # The scrollbar should stick to the top and bottom of its parcel, it need not stick to the
  # left and right.
  tkgrid.configure(scr, sticky="ns")

  # When the window is resized, we want surplus space to be allocated to the text widget,
  # which is in the top left corner of this frame.
  tkgrid.columnconfigure(textbox,0,weight=1)
  tkgrid.rowconfigure(textbox,0,weight=1)

  # status bar and size grip
  statusText <- tclVar("")
  l <- tk2label(root, textvariable=statusText,relief="sunken")
  sg <- ttksizegrip(root)

  # Set up the geometry for the stuff inside the "root" window.

  # First row is just the textbox frame...
  tkgrid(textbox)

  # Second row is the status label and the resize gadget
  tkgrid(l, sg)

  # The textbox widget should span 2 colums, and stick to all four sides of its parcel.
  tkgrid.configure(textbox,columnspan=2,sticky="nsew")

  # The status label should stick to all four sides of its parcel too
  tkgrid.configure(l,sticky="nsew")

  # The resize gadget should only stick to the bottom right of its parcel
  tkgrid.configure(sg,sticky="se")

  # When the window is resized, we want surplus space to go to the textbox frame (and from there
  # to the text widget itself, which it will do thanks to the grid weights we set up above).  The
  # textbox frame is in the top left corner of its parent window.
  tkgrid.columnconfigure(root,0,weight=1)
  tkgrid.rowconfigure(root,0,weight=1)
}

这里有一些关于使用R here中的grid几何管理器的详细信息。

票数 3
EN

Stack Overflow用户

发布于 2011-06-04 23:35:10

如果你坚持打包小部件,你应该意识到,如果没有足够的空间给所有的小部件提供它们所要求的空间,那么空间将优先给第一个打包的小部件(在一个特定的容器中)。首先放入状态栏,然后是滚动条,最后才是主小部件。(您可能需要更改将特定小部件打包到哪一侧,以使其正常工作。)此外,如果它变得太复杂,请记住您可以在frames中打包;这会给您带来很大的灵活性。

但这就是使用网格几何管理器的意义所在。当你要达到应用程序最后10%的外观时,它给了你更多的精细控制,并且需要更少的小部件嵌套来实现它。

票数 2
EN

Stack Overflow用户

发布于 2011-06-04 19:53:04

就像Eric指出的那样,网格无疑是最好的,但是如果你真的想使用pack,那么像下面这样调整扩展的txt小部件的大小,就会得到你想要的东西。有一些大小启发式算法可以改进。

将这段代码添加到代码的末尾:

代码语言:javascript
复制
widthOfChar <- ceiling(as.numeric(tclvalue(tcl("font","measure","TkTextFont","0123456789")))/10) + 2
tkbind(root, "<Configure>", function(W) {
  w.width <- as.integer(tkwinfo("width",W))
  txt.width <- w.width - 15L
  tkconfigure(txt, width=floor(txt.width/widthOfChar))
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6234690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档