2008-10-03 80 views
10

Tcl/Tk是脚本小GUI的一种简单方法。Tcl/Tk的例子?

任何人都可以给一个按钮文本小部件很好的例子。当按下按钮时,应执行shell命令并将输出传送到文本小部件。

如果您有其他有用的任务的很好和干净的例子,请添加它们。

回答

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,您可以使用索引结束,它指刚刚在最后一个换行符后面的位置。例如,

.main insert end "$x\n" 

要将文字滚动,但该命令输出,使用看到命令。例如,添加到。主要文本组件

.main see end 

后,您可能还需要考虑异步抓住命令的输出,通过使用fileevent命令。

+0

太棒了!会尝试... – epatel 2008-10-03 16:51:39