2016-07-07 73 views
1

我想創建一個窗口,其中兩個文本框位於另一個的頂部,首先佔據25%的高度,然後佔據75%的高度。Tcl/tk - 獲取窗口高度和寬度,並在網格中設置相對文本高度

我試圖計算頂級勝利的相對高度/寬度並傳入文本命令,但沒有工作(我猜測是因爲wm幾何體返回的維度單位與傳遞給文本命令時不一樣)

以下是我的代碼:

toplevel .t 
wm geometry .t 1500x800+10+10 
update 
proc topAspect {args} { 
    regexp {(\d+)} $args -> relAspect 
    regexp {([^\d|%]+)} $args -> aspect 
    regexp {(.*)x(.*)[+-](.*)[+-](.*)} [wm geometry .t] -> width height x y 
    puts "width->$width height->$height x->$x y->$y" 
    switch -regexp [string tolower $aspect] { 
     x { 
      return [expr $x + $relAspect] 
     } 
     y { 
      return [expr $y + $relAspect] 
     } 
     w { 
      return [expr $width * $relAspect/100] 
     } 
     h { 
      return [expr $height * $relAspect/100] 
     } 
     default { 
      log::log error "Unsupported relative aspect $aspect cannot be determined for top level window" 
     } 
    } 
} 

text .t.text1 -height [topAspect -width 25%] -width [topAspect -width 99%] 
grid .t.text1 -sticky news 
text .t.text2 -height [topAspect -width 75%] -width [topAspect -width 99%] 
grid .t.text2 -sticky news 

當我嘗試以下 - 它沒有給我一些體面的GUI:

text .t.text1 -height 20 -width [topAspect -width 99%] 
grid .t.text1 -sticky news 
text .t.text2 -height 20 -width [topAspect -width 99%] 
grid .t.text2 -sticky news 

,但我想用relati ve選項。如何使它工作?

回答

2

解決此問題的最簡單方法是使用網格幾何管理器,權重以正確的比例和統一的組。調整窗口大小時它甚至可以正常工作; Tk知道政策本身併爲您維護。 (在內部,grid是一個相當複雜的約束求解;你可以做一些真的複雜的東西吧)

toplevel .t 
grid [text .t.text1 -bg red] -sticky news 
grid [text .t.text2 -bg green] -sticky news 

# The group name is just an arbitrary non-empty string. 
# So long as it is the same on the two rows it will work as desired. 
# The weights give a ratio of 1:3, i.e., 25% to one and 75% to the other. 

grid rowconfigure .t .t.text1 -weight 1 -uniform group1 
grid rowconfigure .t .t.text2 -weight 3 -uniform group1 

(如果你使用Tk的8.5,你需要指定行rowconfigure用數字來代替行中的小部件的經常更方便的名字。)

1

是,爲text插件的-height-width選項在字符爲單位給出的,而不是屏幕單位。你可以通過進一步除以字體寬度和高度來解決這個問題(我將它們設置爲下面的常量值)。請記住,這是整數除法!

Oooh,所有這些正則表達式......我已經清理了一下,你可以拿走它或者離開它。

proc topAspect {aspect relAspect} { 
    set relAspect [string trimright $relAspect %] 
    scan [wm geometry .t] "%dx%d%d%d" width height x y 
    set fontWidth 15 
    set fontHeight 15 
    switch -regexp [string tolower $aspect] { 
     x { 
      return [expr {$x + $relAspect}] 
     } 
     y { 
      return [expr {$y + $relAspect}] 
     } 
     w { 
      return [expr {($width * $relAspect/100)/$fontWidth}] 
     } 
     h { 
      return [expr {($height * $relAspect/100)/$fontHeight}] 
     } 
     default { 
      log::log error "Unsupported relative aspect $aspect cannot be determined for top level window" 
     } 
    } 
} 

而且,你用-width作爲參數傳遞給topAspect兩個-height-width:我相信這是一個錯誤。

text .t.text1 -height [topAspect -height 25%] -width [topAspect -width 99%] 
grid .t.text1 -sticky news 
text .t.text2 -height [topAspect -height 75%] -width [topAspect -width 99%] 
grid .t.text2 -sticky news 

否則,我建議Donal Fellows的解決方案。

文檔: * (operator)+ (operator)/ (operator)exprforgridprocreturnscansetstringswitchtext (widget)wmSyntax of Tcl regular expressions

+0

解析幾何輸出時需要小心,因爲它可能在多監視器設置中具有負偏移。例如:我的「200x200 + -638 + 77」。因此'scan'語句正確地解析了[scan $ geometry'%dx%d +%d +%d「]',它能夠處理底片。 – patthoyts

+0

感謝您的糾正,我誤解了格式的文檔。但從我目前的理解來看,前綴可能是 - 以及+,可能後面跟着數字的符號。如果是這樣,它必須由{%dx%d%* 1 [+ - ]%d%* 1 [+ - ]%d}'或正則表達式{{(\ d +)x(\ d + )[+ - ]([+ - ]?\ d +)[+ - ]([+ - ] \ d +)}'?。 –

+0

格式WxH-X + Y(使用X偏移量的前導減號表示位置來自屏幕右端 - 請參閱tkWinWm.c中有關WM_NEGATIVE_X的註釋。因此,我們可能會得到'1x1- -2 + 3'我認爲是的,看起來像一個正則表達式是必要的 – patthoyts

0

工作地點最好在這種情況下 - 即使是在調整大小下面伸出的比例得好:

place .t.text1 -in .t -relheight .25 -relwidth .98 -relx .003 -rely .003 
place .t.text2 -in .t -relheight .75 -relwidth .98 -relx .003 -rely .254 

是否有任何人在這種方法中看到的相比,電網的任何缺陷。

謝謝

+0

像這樣使用'place'時的主要問題是它不會將大小傳播到包含的小部件。這對你來說可能並不重要,這取決於你的其他GUI中發生了什麼。您還可以通過綁定到右側窗口小部件上的''事件來修復它,但這是事情變得非常複雜的時候。 –

+0

好吧 - 我會按照你的建議和網格一起工作,如果能很好地使用它 - 再次感謝 – BabyGroot

相關問題