10
A
답변
12
다음은 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
3
나는 시작할 수 있습니다 ... 개선을 제안하십시오. 즉 나는
#!/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
4
제안 출력하고는 명령으로 이동하고 싶습니다 : 인덱스를 사용하는 대신 라인 999999를 지정의의 텍스트 위젯 출력을 추가하려면
을 end. 마지막 줄 바꿈 바로 뒤의 위치를 나타냅니다. 예를 들어,
.main insert end "$x\n"
명령이 출력 될 때 상기
가 명령을 참조 사용 텍스트 스크롤을 가지고. 예를 들어, .main 텍스트 위젯
.main see end
에 추가 한 후 또한 fileevent 명령을 사용하여 비동기 명령 출력을 잡는 것이 좋습니다.
1
관련 문제
- 1. 그려지는 동안 R에서 TclTk 창을 숨기는 방법
- 2. 예
- 3. 예 :
- 4. 예
- 5. 예 :
- 6. 예 :
- 7. 예 :
- 8. 예
- 9. 예
- 10. 예 :
- 11. 계산 예
- 12. JQuery와 : 예
- 13. 예 장고
- 14. IQbservable의 예
- 15. GKVoiceChat 예?
- 16. fuzzyCLIPS 예
- 17. MiniBrowser의 예
- 18. contavvariance의 예
- 19. DrawText의 예?
- 20. 예 :? blah =
- 21. 항상 예?
- 22. 플러그인 예
- 23. 문제 예
- 24. TTThumbsViewController 예
- 25. 실행 예
- 26. iPhone - 이전 ModalViewControllerAnimated 닫기 : 예 다음 새 ModalViewControllerAnimated : 예 - 실패합니다.
- 27. NSString - 숫자 (예 : '1')를 숫자 (예 : '1')로 변환합니다.
- 28. 웹 사이트 UML 예
- 29. 사양 패턴 예
- 30. 디스플레이, 예 : Ó
큰 모든 종류의 좋은 웹 사이트입니다! 그것을 시도 할 것이다 ... – epatel