Tcl & Tk/팁 (Tip)

윈도우 always on top 구현

티클러 2025. 3. 17. 14:47

리눅스의 경우 아래와 같이 합니다.

# -----------------------------------------
# always on top for linux
# -----------------------------------------
proc ontop {W boolean} {
    set bindtags [bindtags $W]
    set index [lsearch $bindtags bind$W]
    if { $boolean } {
        if { $index == -1 } {
            bindtags $W [linsert $bindtags 0 bind$W]
            bind bind$W <Visibility> [list ontop:state %W %s]
        }
    } else {
        if { $index != -1 } {
            bindtags $W [lreplace $bindtags $index $index]
            bind bind$W <Visibility> {}
        }
    }
}
proc ontop:state {W state} {
    #puts "ontop \[$W\] \[$state\]"
    if { ![string equal [winfo toplevel $W] $W] } {
        #puts "  window \[$W\] is not a toplevel"
        return
    }
    if { ![string equal $state VisibilityUnobscured] } { raise $W }
}

# usage: ontop .win 1

윈도즈의 경우 아래아 같이 합니다.

 wm attrib .win -topmost 1

 

두 코드를 동시 지원은 다음과 같이 합니다.

if { $::tcl_platform(platform) == "windows" } {
    wm attrib .win -topmost 1
} else {
    ontop .win 1
}

 

두 플랫폼에서 테스트되었습니다.