通过线程将文本插入到tcl tk中的文本框中

时间:2016-06-03 08:06:23

标签: multithreading tcl tk

我目前正在开发tcl tk中的gui。在gui我试图连续读取文件(tail -f)。我想将数据放入文本框中。我已经创建了用于分离这个taks并保持gui活着的线程。但它不允许我在线程中使用文本小部件名称。

所以我一直坚持如何将文本小部件名称传递给线程的问题。

ttk::notebook .note -width 1000 -height 450
ttk::frame .note.tabOne; 
ttk::frame .note.tabTwo; 

.note add .note.tabOne -text "Test Configuration" 
.note add .note.tabTwo -text "Results"

set .note.tabTwo.results [ScrolledWindow .note.tabTwo.results ]
pack .note.tabTwo.results -fill both -expand true

set resulttxt [text .note.tabTwo.results.resulttxt -wrap none -width 63 -height 10]
.note.tabTwo.results setwidget $resulttxt
pack .note

proc displayText {$file} { 
    global file
    global resulttxt
    set data "hithi fio ui"
    $resulttxt insert end $data
    exec fio $file --status-interval=1 > test1.txt 2>&1 &

    set t [thread::create]
    thread::send -async $t [list set yourxs $resulttxt]

    thread::send $t {
    global resulttxt
    puts $yourxs
    # Load the Tcllib fileutil package to use its
    # findByPattern procedure.
    #package require fileutil
    #set files [fileutil::findByPattern [pwd] *.tcl]
    set log [open /media/sf_Tcl/bin/project/fio_ui-1.0/test1.txt r]

    for { } { true } { after 1000 } {
        #after [expr { int(500*rand()) }]
        $yourxs insert end  $data1

    }

    close $log
   }
}

1 个答案:

答案 0 :(得分:0)

在Tcl中,所有命令名称(包括所有窗口小部件名称)对于特定的解释器实例是本地的,并且实例绑定到线程。非GUI线程必须将代码发送到GUI线程以进行插入。处理此问题的最简单方法是使GUI线程在非GUI线程中创建命令以进行插入。

thread::send $t [list proc appendToTextWidget string \
        "thread::send [thread::id] .note.tabTwo.results.resulttxt end \$string"]

所需的引用有点棘手,因为你不能只使用list来完成所有的工作,但它是相当直接的,因为thread::id保证返回一个无元字符的单词

完成后,非GUI线程就可以:

appendToTextWidget "here's a message\n"

这将在最后添加文本,隐藏处理代码中发生的所有细节。