2014-04-11 56 views
0

我無法從小部件傳播到父窗口的事件。我的意思是我有自己的命令紡紗器,和我的鼠標點擊頂層綁定退出應用程序tcl/tk小部件命令和綁定鼠標按鈕單擊衝突

spinbox .mask -command { remask this %d ; break } 
bind . <ButtonRelease-1> { recopy this ; exit } 

當我點擊紡紗器滾動按鈕時,應用程序退出所有的時間。 breakbox似乎在spinbox事件命令中不起作用。

上下鍵綁定也不起作用。

這裏是爲了方便完整的代碼

proc expand { name } { 
    upvar 2 $name what 
    uplevel "upvar 1 $name that" 
    foreach key [array names what] { 
     uplevel "upvar 0 that($key) $key" 
    } 
} 

proc remask { name { delta 0 } } { 
    expand $name 

    if {$delta == "up" } { set delta 1 } 
    if {$delta == "down"} { set delta -1 } 
    set delta [expr $delta < 0 ? -1 : $delta > 0 ? 1 : 0] 
    set date [clock add $date $delta $unit] 
    set file [clock format $date -format $mask -locale fr] 
    set test [file exists $file] 
} 

proc reunit { name } { 
    expand $name 

    set map { 
    %a days 
    %A days 
    %d days 
    %V weeks 
    %b months 
    %B months 
    %m months 
    %y years 
    %Y years 
    %g years 
    %G years 
    } 

    foreach {key value} $map { 
    if { [string first $key $mask] > 0 } { 
     set unit $value 
     break 
    } 
    } 
} 

proc recopy { name } { 
    expand $name 

    if {!$test} { 
     if [file exists $mask] { 
      file copy $mask $file 
     } else { 
      open $file w 
     } 
    } 
} 

array set map { 
    0 green 
    1 red 
} 

set taken 0 
if {$argc > 1} { 
    set path [lindex $argv 0] 
    cd [file dirname $path] 
} else { 
    set path "file_%Y%m%d.txt" 
} 
set this(mask) [file tail $path] 
set this(file) $this(mask) 
set this(date) [clock seconds] 
set this(unit) days 
set this(test) [file exists $this(file)] 

reunit this 
remask this 

set posx [winfo pointerx .] 
set posy [winfo pointery .] 
set posx [expr $posx - 50] 
set posy [expr $posy - 40] 

wm title . "rename $this(mask)" 
wm attributes . -toolwindow true 
wm geometry . +$posx+$posy 

spinbox .mask -textvariable this(file) -foreground $map($this(test)) -command { remask this %d ; break } 
pack .mask -fill x -padx 20 -pady 5 

grab . 
bind . <ButtonRelease-1> { recopy this ; exit } 
bind . <Key-Return>  { recopy this ; exit } 
bind . <ButtonRelease-3> { exit } 
bind . <Key-Escape>  { exit } 
bind . <MouseWheel>  { remask this %D ; .mask configure -foreground $map($this(test)) } 
bind . <Key-plus>  { remask this +1 ; .mask configure -foreground $map($this(test)) } 
bind . <Key-Up>   { remask this +1 ; .mask configure -foreground $map($this(test)) } 
bind . <Key-minus>  { remask this -1 ; .mask configure -foreground $map($this(test)) } 
bind . <Key-Down>  { remask this -1 ; .mask configure -foreground $map($this(test)) } 

回答

1

的核心問題是break只有在綁定腳本神奇,而不是在-command選項腳本,因爲這是東西是隻間接調用一個綁定腳本(通過類級綁定)。這不會改變。 (另外,-command選項也不會做% -bind-替代正常,儘管它可能替代;見小部件文檔。)

爲了防止部件從傳播<ButtonRelease-1>事件(,只是該事件)到包含頂層,您需要插入額外的binding tag並在那裏應用綁定。

spinbox .mask -command { remask this %d } 
bind . <ButtonRelease-1> { recopy this ; exit } 
bindtags .mask {.mask Spinbox extraMagic.mask . all} 
bind extraMagic.mask <ButtonRelease-1> break 

結合標記字符串extraMagic.mask不是特別在所有除了它是在bindbindtags命令都提及。 (我把這個小部件的名稱放在最後,因爲我正在考慮將它應用於單個小部件。)特別的是它介於Spinbox(類綁定標記)和.(上層綁定標籤),並且它具有<ButtonRelease-1>的綁定,其確實是break

請注意,這隻適用於傳遞到特定紡絲箱的事件;單擊頂層中的任何其他窗口小部件都會使事情退出。 (通常建議僅將加速鍵綁定在頂級平臺上,並且用戶代碼根本不要附加到all,因爲全局綁定非常微妙,適用於容易忘記的地方)。

+0

完美解釋很清楚,不僅僅是文件。感謝你我現在明白綁定標籤的東西。 – buzard

+0

我忘記了bindtags的規範解釋在哪裏;它甚至可能在一本書中... –