无法在笔记本中向下滚动

时间:2016-08-26 11:26:30

标签: tcl tk

我创建了一个tcl笔记本,它有两个标签,如果文件太大,我无法向下滚动。在这里分享我的代码。 要执行此代码,您需要2个文件(Warning.txt和Error.txt),在运行此代码之前将它们放在同一目录中。 请创建两个文件Error.txt和Warning.txt。 把一些内容放到它上面就可以得到10000行。

#!/usr/bin/wish -f
#
package require Tk

proc noteb {} {
    global rundir logfile
    frame .lpo
    pack .lpo -side top -fill both -expand true
    #cd $rundir
    set ft [exec grep -c Warning ./Warning.txt]
    puts $ft
    set ert [exec grep -c Error ./Error.txt]
    puts $ert
    pack [frame .fa] -fill both -side top
    pack [ttk::notebook .fa.nb] -fill both
    set gt "Errors"
    set bt "Warnings"
    set delim ":"
    set rt [concat [string trim $bt][string trim $delim][string trim $ft]]
    set dt [concat [string trim $gt][string trim $delim][string trim $ert]]

    if {$ft > 0} {
        .fa.nb add [frame .fa.nb.f1] -text $rt
    } else {
        .fa.nb add [frame .fa.nb.f1] -text "Warnings"
    }
    pack [frame .fa.nb.f1.f11] -side top -fill both -expand true
    pack [text .fa.nb.f1.f11.t1 -bg LightYellow -borderwidth 2 -width 80 -height 6 -relief raised -setgrid true ] -side left -fill both -expand true
    scrollbar .fa.nb.f1.f11.scroll -command {.fa.nb.f1.f11.t1 yview}
    pack .fa.nb.f1.f11.scroll -side right -fill y
    set fp1 [open Warning.txt r]
    set v [read $fp1]
    .fa.nb.f1.f11.t1 insert 1.0 $v
    close $fp1

    if {$ert > 0} {
        .fa.nb add [frame .fa.nb.f2] -text $dt
    } else {
        .fa.nb add [frame .fa.nb.f2] -text "Errors"
    }

    pack [frame .fa.nb.f2.f11] -side top  -fill both -expand true
    pack [text .fa.nb.f2.f11.t1 -bg LightYellow -borderwidth 2  -width 80 -height 6 -relief raised -setgrid true ] -side left -fill both -expand true
    scrollbar .fa.nb.f2.f11.scroll -command {.fa.nb.f2.f11.t1 yview}
    pack .fa.nb.f2.f11.scroll -side right -fill y
    set fp [open Error.txt r]
    set c [read $fp]
    .fa.nb.f2.f11.t1 insert 1.0 $c
    close $fp
}

button .mn -text summary -command {noteb}
pack .mn -side left

2 个答案:

答案 0 :(得分:2)

在下面的这些行中,您可以告诉滚动条有关文本小部件的信息,而不是滚动条的文本小部件。你需要做到这两点。

pack [text .fa.nb.f1.f11.t1 -bg LightYellow -borderwidth 2 -width 80 -height 6 -relief raised -setgrid true ] -side left -fill both -expand true
scrollbar .fa.nb.f1.f11.scroll -command {.fa.nb.f1.f11.t1 yview}
pack .fa.nb.f1.f11.scroll -side right -fill y

让它改为:

text .fa.nb.f1.f11.t1 -bg LightYellow -borderwidth 2 -width 80 -height 6 \
        -relief raised -setgrid true -yscroll {.fa.nb.f1.f11.scroll set}
scrollbar .fa.nb.f1.f11.scroll -command {.fa.nb.f1.f11.t1 yview}

pack .fa.nb.f1.f11.t1 -side left -fill both -expand true
pack .fa.nb.f1.f11.scroll -side right -fill y

虽然我实际上倾向于这样做:

set w .fa.nb.f1.f11
text $w.t1 -bg LightYellow -borderwidth 2 -width 80 -height 6 -relief raised \
        -setgrid true -yscroll [list $w.scroll set]
scrollbar $w.scroll -command [list $w.t1 yview]

pack $w.t1 -side left -fill both -expand true
pack $w.scroll -side right -fill y

将容器窗口小部件名称放在变量中有助于使代码更加合理,并使文本和滚动条更加明显相关。 (如果需要,它还可以更容易地重构代码。)

答案 1 :(得分:1)

我认为如果在文本小部件上设置-yscroll选项,它将起作用。

相关问题