如何在 Tcl/Tk 中灵活调整文本小部件的大小
How to have flexible size of a text widget in Tcl/Tk
我正在处理一个问题:我不知道如何使用 window 调整我的文本区域。当 window 放大时,文本区域保持其初始大小并且有很多空白区域。
我的 window 的定义如下:
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
}
几何管理器的"pack"命令应该不错,但我不知道怎么用,但肯定还有其他我不知道的解决方案。
我想让文本区域和问题框架的大小灵活。
有人可以帮助我吗?
您需要使包含 text
的 grid
单元格双向展开。为此,必须为包含单元格的行和包含单元格的列赋予非零权重。
因此,之后:
grid .fr.txt -column 0 -row 1 -sticky snwe
添加这些:
grid rowconfigure .fr 1 -weight 1
grid columnconfigure .fr 0 -weight 1
请记住,您正在主窗口小部件中配置网格的行和列。
此外,如果该主小部件本身是 frame
,请确保它也会扩展以填充它可用的 space。
如果您使用的是 pack
而不是 grid
pack 的等价物是:
pack .fr.txt -expand 1 -fill both
虽然 pack
和 grid
之间存在许多差异,但两者都要求您说明何时希望小部件占用 space 可用空间。此外,当事情变得复杂时,它可以帮助 lot 使每个小部件(暂时!)具有不同颜色的背景,因此您可以看到它实际在做什么。
我正在处理一个问题:我不知道如何使用 window 调整我的文本区域。当 window 放大时,文本区域保持其初始大小并且有很多空白区域。 我的 window 的定义如下:
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
}
几何管理器的"pack"命令应该不错,但我不知道怎么用,但肯定还有其他我不知道的解决方案。 我想让文本区域和问题框架的大小灵活。 有人可以帮助我吗?
您需要使包含 text
的 grid
单元格双向展开。为此,必须为包含单元格的行和包含单元格的列赋予非零权重。
因此,之后:
grid .fr.txt -column 0 -row 1 -sticky snwe
添加这些:
grid rowconfigure .fr 1 -weight 1
grid columnconfigure .fr 0 -weight 1
请记住,您正在主窗口小部件中配置网格的行和列。
此外,如果该主小部件本身是 frame
,请确保它也会扩展以填充它可用的 space。
如果您使用的是 pack
而不是 grid
pack 的等价物是:
pack .fr.txt -expand 1 -fill both
虽然 pack
和 grid
之间存在许多差异,但两者都要求您说明何时希望小部件占用 space 可用空间。此外,当事情变得复杂时,它可以帮助 lot 使每个小部件(暂时!)具有不同颜色的背景,因此您可以看到它实际在做什么。