如何在Tcl / Tk中拥有灵活的文本小部件大小

时间:2015-08-18 10:22:43

标签: widget tcl tk

我正在处理一个问题:我不知道如何通过窗口调整文本区域。当窗口放大时,文本区域保持其初始大小,并且有很多空区域。 我的窗口定义如下:

proc windowDef {} {

    destroy .frq

    # define the main text zone
    text .fr.txt -wrap word -yscroll {.fr.vscroll set} -highlightthickness 0
    scrollbar .fr.vscroll -orient vertical -command {.fr.txt yview}
    grid .fr.txt -column 0 -row 1 -sticky snwe
    grid .fr.vscroll -column 1 -row 1 -sticky snwe

    # define the canvas for interactive diagrams
    canvas .fr.canv -bg #f8f8f8 -yscrollcommand {.fr.vscroll set} -scrollregion "0 0 750 750"

    .fr.txt tag configure center -justify center
    .fr.txt configure -font "times 12" -state disabled

    .fr.vscroll configure -orient vertical -command {.fr.txt yview}
    grid .fr.txt -column 0 -row 1 -sticky snwe
    .fr.txt configure -state normal
    .fr.txt delete 1.0 end

    #questions frame
    frame .frq
    frame .frq.fr2 -background white
    grid .frq.fr2 -row 1

    label .frq.lbl -relief flat -font arial -text "Question 1 of 5"
    grid .frq.lbl -row 0
    text .frq.fr2.txt -relief flat -background white -font "times 13" -wrap word
    grid .frq.fr2.txt -columnspan 2 -row 1
    .frq.fr2.txt tag configure center -justify center
    grid rowconfigure .frq 1 -weight 1

    checkbutton .frq.fr2.cb1 -command "choose A" -cursor hand1
    grid .frq.fr2.cb1 -column 0 -row 2
    label .frq.fr2.lblA -background white -font "arial 12" -pady 3
    bind .frq.fr2.lblA <ButtonRelease> "choose A; .frq.fr2.cb1 toggle"
    grid .frq.fr2.lblA -column 1 -row 2 -sticky w
    checkbutton .frq.fr2.cb2 -command "choose B" -cursor hand1
    grid .frq.fr2.cb2 -column 0 -row 3
    label .frq.fr2.lblB -background white -font "arial 12" -pady 3
    bind .frq.fr2.lblB <ButtonRelease> "choose B; .frq.fr2.cb2 toggle"
    grid .frq.fr2.lblB -column 1 -row 3 -sticky w
    checkbutton .frq.fr2.cb3 -command "choose C" -cursor hand1
    grid .frq.fr2.cb3 -column 0 -row 4
    label .frq.fr2.lblC -background white -font "arial 12" -pady 3
    bind .frq.fr2.lblC <ButtonRelease> "choose C; .frq.fr2.cb3 toggle"
    grid .frq.fr2.lblC -column 1 -row 4 -sticky w
    checkbutton .frq.fr2.cb4 -command "choose D" -cursor hand1
    grid .frq.fr2.cb4 -column 0 -row 5
    label .frq.fr2.lblD -background white -font "arial 12" -pady 3
    bind .frq.fr2.lblD <ButtonRelease> "choose D; .frq.fr2.cb4 toggle"
    grid .frq.fr2.lblD -column 1 -row 5 -sticky w

    frame .frq.bar
    grid .frq.bar -row 2
    button .frq.bar.next -text "Next question >>" -state disabled -pady 5 -borderwidth 0 -command "set ::goOn 1"
    pack .frq.bar.next -padx 5 -pady 5 -side right -fill none
    button .frq.bar.dp -text "Open drawing pad" -pady 5 -borderwidth 0 -command notepad
    pack .frq.bar.dp -padx 5 -pady 5 -side right -fill none
    button .frq.bar.calc -text "Open calculator" -pady 5 -borderwidth 0 -command calculator
    pack .frq.bar.calc -padx 5 -pady 5 -side right -fill none
    button .frq.bar.quit -text "Quit test" -pady 5 -borderwidth 0 -command "set ::stop 1; set ::goOn 1"
    pack .frq.bar.quit -padx 5 -pady 5 -side right -fill none

    # title of section
    label .fr.titl
    .fr.titl configure -font "arial 20" -pady 10
    grid .fr.titl -column 0 -row 0 -sticky swe
    .fr.titl configure -background "White"

    #text styles
    .fr.txt tag configure Normal -font "times 12"
    .fr.txt tag configure subTitle -font "times 14"
    .fr.txt tag configure Titlec -font "times 16" -justify "center"
    .fr.txt tag configure subTitlec -font "times 14" -justify "center"
    .fr.txt tag configure subTitlecu -font "times 14"  -justify "center" -underline on
    .fr.txt tag configure Titlecu -font "times 16" -justify "center" -underline on
    .fr.txt tag configure Title -font "times 16"
    .fr.txt tag configure link -foreground blue -font "times 12"
    .fr.txt tag configure right -foreground "forest green"
    .fr.txt tag configure wrong -foreground red
    .fr.txt tag configure enhance -background "light goldenrod"
    .fr.txt tag configure rightenhance -background "light goldenrod" -foreground "forest green"
    .fr.txt tag bind link <Enter> ".fr.txt configure -cursor hand1"
    .fr.txt tag bind link <Leave> ".fr.txt configure -cursor arrow"
}

其他程序会调用它,如下所示:

proc loadBackground {} {
    #variables
    set ::i 0
    set close 0
    set fd [open [pwd]/content/background r]
    set ::data [split [read $fd] \n]
    close $fd

    #window definition
    toplevel .fr
    wm title .fr "Background" 
    wm geometry .fr 750x750+0+105
    .fr configure -bg $::bgColour
    windowDef

    dispFile [lindex $::data $::i]

    #buttons definition 
    frame .fr.bar
    grid .fr.bar -row 2
    button .fr.bar.bk -text "<< Back" -pady 5 -borderwidth 0 -command backFile
    pack .fr.bar.bk -padx 5 -pady 5 -side left -fill none
    button .fr.bar.cl -text "Close" -pady 5 -borderwidth 0 -command { set close 1}
    pack .fr.bar.cl -padx 5 -pady 5 -side left -fill none
    button .fr.bar.nx -text "Next >>" -pady 5 -borderwidth 0 -command nextFile
    pack .fr.bar.nx -padx 5 -pady 5 -side right -fill none

    vwait close
    destroy .fr
    destroy .frq
}

&#34; pack&#34;几何管理器的命令应该是好的,但我不知道如何使用它,但必须有其他我不知道的解决方案。 我希望文本区域和问题框架的大小灵活。 有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

您需要使包含grid的{​​{1}}单元格在两个方向上展开。为此,包含单元格的行和包含单元格的列都必须赋予非零权重。

因此,在:

之后
text

添加以下内容:

    grid .fr.txt -column 0 -row 1 -sticky snwe

请记住,您正在主窗口小部件中配置网格的行和列。

此外,如果该主要窗口小部件本身是 grid rowconfigure .fr 1 -weight 1 grid columnconfigure .fr 0 -weight 1 ,请确保它也会展开以填充可用空间。

如果您使用的是frame而不是pack

与pack相同的东西是:

grid

虽然 pack .fr.txt -expand 1 -fill both pack之间存在许多差异,但两者都要求您说明何时希望窗口小部件占用可用空间。此外,当事情变得复杂时,它可以帮助批次使每个小部件(暂时!)具有不同的彩色背景,因此您可以看到它实际上在做什么。