2008-10-03 15 views
10

Tcl/Tk छोटे जीयूआई स्क्रिप्ट करने का एक आसान तरीका है।टीसीएल/उदाहरण उदाहरण?

क्या कोई बटन और टेक्स्ट विजेट के साथ एक अच्छा उदाहरण दे सकता है। जब बटन दबाया जाता है तो एक शेल कमांड निष्पादित किया जाना चाहिए और पाठ विजेट पर आउटपुट पाइप किया जाना चाहिए।

यदि आपके पास उपयोगी कार्यों के लिए अन्य अच्छे और साफ उदाहरण हैं, तो कृपया उन्हें भी जोड़ें।

उत्तर

12

फ़ाइलवृत्तियों का उपयोग करके यहां एक और पूर्ण उदाहरण है। यह हर समय स्वतः स्क्रॉल करेगा। प्रयोज्य प्रयोजनों के लिए यदि आप टेक्स्ट के नीचे दिखाई दे रहे हैं तो शायद आप केवल ऑटो-स्क्रॉल करना चाहते हैं (यानी: यदि उपयोगकर्ता स्क्रॉलबार नहीं ले गया है) लेकिन मैं पाठक के लिए इसे पहले से ही लंबे उदाहरण के लिए अभ्यास के रूप में छोड़ दूंगा अब और प्राप्त करने से।

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

कुछ सुझाव outputting है: बजाय लाइन 999999 निर्दिष्ट करने के,

पाठ विजेट के उत्पादन संलग्न करने के लिए आपको सूचकांक का उपयोग कर सकते अंत, जो कि अंतिम नई लाइन के बाद स्थिति को संदर्भित करता है। उदाहरण के लिए,

.main insert end "$x\n" 

पाठ पुस्तक के रूप में आदेश outputting है, का उपयोग आदेश को देखने करवाने के लिए। उदाहरण के लिए, मुख्य पाठ विजेट

.main see end 

को जोड़कर के बाद आप यह भी आदेश उत्पादन एसिंक्रोनस रूप से हथियाने, fileevent कमांड का उपयोग करके विचार कर सकते हैं।

+0

महान के सभी प्रकार के लिए अच्छा वेबसाइट है! कोशिश करेंगे ... – epatel