문제

TCL/TK 작은 GUI를 스크립팅하는 간단한 방법입니다.

누구든지 좋은 모범을 보일 수 있습니까? 단추 그리고 a 텍스트 위젯. 버튼을 누르면 쉘 명령이 실행되고 출력이 텍스트 위젯.

유용한 작업에 대한 다른 멋지고 깨끗한 예제가 있으면 추가하십시오.

도움이 되었습니까?

해결책

다음은 FileEvents를 사용하는보다 완전한 예입니다. 이것은 항상 자동 스크롤됩니다. 유용성 목적을 위해 텍스트의 맨 아래가 보이는 경우 (예 : 사용자가 스크롤 바를 이동하지 않은 경우) 독자가 이미이 긴 예를 유지하기위한 연습으로 남겨두기를 원할 것입니다. 더 이상 얻는 것입니다.

package require Tk

proc main {} {
    if {[lsearch -exact [font names] TkDefaultFont] == -1} {
        # older versions of Tk don't define this font, so pick something
        # suitable
        font create TkDefaultFont -family Helvetica -size 12
    }
    # in 8.5 we can use {*} but this will work in earlier versions
    eval font create TkBoldFont [font actual TkDefaultFont] -weight bold

    buildUI
}

proc buildUI {} {
    frame .toolbar
    scrollbar .vsb -command [list .t yview]
    text .t \
        -width 80 -height 20 \
        -yscrollcommand [list .vsb set] \
        -highlightthickness 0
    .t tag configure command -font TkBoldFont
    .t tag configure error   -font TkDefaultFont -foreground firebrick
    .t tag configure output  -font TkDefaultFont -foreground black

    grid .toolbar -sticky nsew
    grid .t .vsb  -sticky nsew
    grid rowconfigure . 1 -weight 1
    grid columnconfigure . 0 -weight 1

    set i 0
    foreach {label command} {
        date     {date} 
        uptime   {uptime} 
        ls       {ls -l}
    } {
        button .b$i -text $label -command [list runCommand $command]
        pack .b$i -in .toolbar -side left
        incr i
    }
}

proc output {type text} {
    .t configure -state normal
    .t insert end $text $type "\n"
    .t see end
    .t configure -state disabled
}

proc runCommand {cmd} {
    output command $cmd
    set f [open "| $cmd" r]
    fconfigure $f -blocking false
    fileevent $f readable  [list handleFileEvent $f]
}

proc closePipe {f} {
    # turn blocking on so we can catch any errors
    fconfigure $f -blocking true
    if {[catch {close $f} err]} {
        output error $err
    }
}

proc handleFileEvent {f} {
    set status [catch { gets $f line } result]
    if { $status != 0 } {
        # unexpected error
        output error $result
        closePipe $f

    } elseif { $result >= 0 } {
        # we got some output
        output normal $line

    } elseif { [eof $f] } {
        # End of file
        closePipe $f

    } elseif { [fblocked $f] } {
        # Read blocked, so do nothing
    }
}


main

다른 팁

몇 가지 제안 :

출력을 텍스트 위젯, 999999 행을 지정하는 대신 인덱스를 사용할 수 있습니다. , 마지막 Newline 직후 위치를 나타냅니다. 예를 들어,

.main insert end "$x\n"

명령이 출력 될 때 텍스트 스크롤을 사용하려면 보다 명령. 예를 들어 .main 텍스트 위젯에 추가 된 후

.main see end

당신은 또한 파일 이벤트 명령.

시작할 수 있습니다 ... 개선을 제안하십시오. 즉 명령이 출력되므로 스크롤하고 싶습니다.

#!/usr/bin/wish

proc push_button {} {
    put_text
    .main see end
}

proc put_text {} {
  set f [ open "| date" r]
  while {[gets $f x] >= 0} {
    .main insert end "$x\n"    
  }
  catch {close $f}
}

button .but -text "Push Me" -command "push_button"
text .main -relief sunken -bd 2 -yscrollcommand ".scroll set"
scrollbar .scroll -command ".main yview"

pack .but
pack .main -side left -fill y
pack .scroll -side right -fill y

wiki.tcl.tk 모든 종류의 좋은 웹 사이트입니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top