Tcl/Tkは、小さな GUI をスクリプト化する簡単な方法です。
ボタンとテキストウィジェットを使った良い例を誰でも挙げることができますか? ボタンが押されると、シェル コマンドが実行され、出力がテキストウィジェットにパイプされます。
便利なタスクの他の素敵でクリーンな例がある場合は、それらも追加してください。
Tcl/Tkは、小さな GUI をスクリプト化する簡単な方法です。
ボタンとテキストウィジェットを使った良い例を誰でも挙げることができますか? ボタンが押されると、シェル コマンドが実行され、出力がテキストウィジェットにパイプされます。
便利なタスクの他の素敵でクリーンな例がある場合は、それらも追加してください。
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行を指定する代わりに、出力をテキストウィジェットに追加するには、最後の改行の直後の位置を参照するindexendを使用できます。例えば、
.main insert end "$x\n"
コマンドの出力時にテキストをスクロールさせるには、seeコマンドを使用します。たとえば、.mainテキストウィジェットに追加した後
.main see end
fileeventコマンドを使用して、コマンド出力を非同期で取得することを検討することもできます。
私は始めることができます...改善を提案してください。つまり、コマンドが出力されているときにスクロールしたい
#!/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は、あらゆる種類の例に適した Web サイトです。