PiXieBulles
PiXieBulles

Reputation: 63

How to have flexible size of a text widget in Tcl/Tk

I am dealing with a problem: I don't know how to have my text area adjustable with the window. When the window is enlarged, the text zone remain at its initial size and there is a lot of empty zone. My window's definition is following:

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"
}

It is called by other procedures like the one following:

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
}

The "pack" command of the geometry manager should be good, but I don't know how I can use it, yet there must be other solution I don't know. I want to have the size of the text zone and the question frame flexible. Someone can help me?

Upvotes: 0

Views: 1293

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137587

You need to make the grid cell containing the text expand in both directions. To do that, both the row containing the cell and column containing the cell have to be given non-zero weights.

Thus, after:

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

add these:

    grid rowconfigure .fr 1 -weight 1
    grid columnconfigure .fr 0 -weight 1

Remember, you're configuring the row and column of the grid in the master widget.

Also, if that master widget is a frame itself, make sure that it will also expand to fill the space available to it.

If you were using pack instead of grid

There are equivalent thing with pack would have been:

    pack .fr.txt -expand 1 -fill both

While there are many differences between pack and grid, both require you to say when you want a widget to occupy space available to it. Also, when things get complicated, it can help a lot to make each widget (temporarily!) have a different coloured background, so you can see what it is actually doing.

Upvotes: 1

Related Questions