如何隐藏文本小部件并稍后在 tcl tk 中显示它?
How to hide a text widget and show it later in tcl tk?
我的多项选择题显示有问题。我有一个框架,它显示了文本小部件上方的问题和选择,该小部件显示了所选答案是否正确。但是我想在测试未完成时隐藏文本。
它看起来像这样:
我的程序如下:
proc beginTest { number } {
startTest
.frq.txt configure -state normal
.frq.txt delete 1.0 end
set score 0
set save 0
set path Files/Tutorials/$::Tuto
set numbers { 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 }
for {set i 1} {$i<=10} {incr i} {
if { $::stop == 1 } {
set answer [tk_dialog .dialog "Quit test?" "If you quit the test now, your answers will be lost. Do you want to continue?" \
question 1 "Continue test" "Quit test"]
if {$answer == 0} {
continue
} else {
break
}
} else {
set ind [expr {int(rand()*[llength $numbers])} ]
incr score [displayQuestion $path [lindex $numbers $ind] $i 10]
set numbers [lreplace $numbers $ind $ind]
}
}
.frq.txt insert end "Your score: $score/10"
.frq.txt configure -state disabled
if {[info exist user]} {
if {[lindex $::testScores $number]== "Not Taken" || [lindex $::testScores $number] < $score} {
set ::testScores [lreplace $::testScores $number $number $score]
}
}
destroy .frq.fr2
}
在前一个程序的第一行调用的以下过程创建并定义 windows、框架、文本区域等
proc startTest {} {
destroy .fr
#window definition
toplevel .frq
wm title .frq "Test"
wm geometry .frq 740x670+0+0
.frq configure -bg $::bgColour
# title of section
label .frq.titl
.frq.titl configure -font "arial 20" -pady 10
grid .frq.titl -column 0 -row 0 -sticky swe
.frq.titl configure -background "White"
# define the main text zone
text .frq.txt -wrap word -yscroll {.frq.vscroll set} -highlightthickness 0
.frq.txt configure -background "White" ;#"slate gray" to debug
scrollbar .frq.vscroll -orient vertical -command {.frq.txt yview}
grid .frq.txt -column 0 -row 1 -sticky snwe
grid rowconfigure .frq 1 -weight 1
grid columnconfigure .frq 0 -weight 1
grid .frq.vscroll -column 1 -row 1 -sticky snwe
.frq.txt tag configure center -justify center
.frq.txt configure -font "times 12" -state disabled
.frq.vscroll configure -orient vertical -command {.frq.txt yview}
grid .frq.txt -column 0 -row 1 -sticky snwe
.frq.txt configure -state normal
.frq.txt delete 1.0 end
#text styles
.frq.txt tag configure Normal -font "times 12"
.frq.txt tag configure subTitle -font "times 14"
.frq.txt tag configure Titlec -font "times 16" -justify "center"
.frq.txt tag configure subTitlec -font "times 14" -justify "center"
.frq.txt tag configure subTitlecu -font "times 14" -justify "center" -underline on
.frq.txt tag configure Titlecu -font "times 16" -justify "center" -underline on
.frq.txt tag configure Title -font "times 16"
.frq.txt tag configure link -foreground blue -font "times 12"
.frq.txt tag configure right -foreground "forest green"
.frq.txt tag configure wrong -foreground red
.frq.txt tag configure enhance -background "light goldenrod"
.frq.txt tag configure rightenhance -background "light goldenrod" -foreground "forest green"
.frq.txt tag bind link <Enter> ".fr.txt configure -cursor hand1"
.frq.txt tag bind link <Leave> ".fr.txt configure -cursor arrow"
#questions frame
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
}
最后,显示问题的程序:
proc displayQuestion {path filename ind maxind} {
set fd [open $path/$filename r]
set questionFile [split [read $fd] "\n"]
set ::goOn 0
set answers [list]
set imgFlag 0
set fd [open $path/answers/$filename r]
set goodAnswer [read $fd]
close $fd
.frq.lbl configure -text "Question $ind of $maxind"
.frq.fr2.txt configure -state normal
.frq.fr2.txt delete 1.0 end
.frq.fr2.txt insert end \n
foreach line $questionFile {
if {$line == "<image>"} {
set imgFlag 1
continue
}
if {$imgFlag == 1} {
set imgFlag 0
.frq.fr2.txt insert end " " center
.frq.fr2.txt image create end -image [image create photo -file [file join "$path/$line"]]
continue
}
if {[string match A)* $line]} {
.frq.fr2.lblA configure -text $line
lappend answers $line
continue
}
if {[string match B)* $line]} {
.frq.fr2.lblB configure -text $line
lappend answers $line
continue
}
if {[string match C)* $line]} {
.frq.fr2.lblC configure -text $line
lappend answers $line
continue
}
if {[string match D)* $line]} {
.frq.fr2.lblD configure -text $line
lappend answers $line
continue
}
.frq.fr2.txt insert end $line\n center
}
.frq.fr2.txt configure -state disabled
.frq.txt insert end [lindex $questionFile 0]\n
.frq.fr2.cb1 deselect
.frq.fr2.cb2 deselect
.frq.fr2.cb3 deselect
.frq.fr2.cb4 deselect
.frq.bar.next configure -state disabled
set ::choice 0
vwait ::goOn
switch $::choice {
A { set ind 0 }
B { set ind 1 }
C { set ind 2 }
default { set ind 3 }
}
if { $::choice == $goodAnswer} {
.frq.txt insert end "Correct answer: [lindex $answers $ind]\n\n" right
return 1
} else {
switch $goodAnswer {
A { set i 0 }
B { set i 1 }
C { set i 2 }
default { set i 3 }
}
.frq.txt insert end "Wrong answer: [lindex $answers $ind]\n\tYou should have pick [lindex $answers $i]\n\n" wrong
return 0
}
}
相关的小部件是“.frq.txt”。
我尝试了 "pack" 和 "pack forget",甚至尝试了一种使用选项“-elide”的方法,但我没有想法。
你能帮帮我吗?
由于您使用 grid
来管理 .frq
小部件的内容,包括 .frq.txt
小部件,因此您需要使用 grid remove
或 grid forget
使小部件消失。两者的区别?使用 grid remove
,将记住单元格的大小调整元数据。
pack forget
用于撤消 pack
(严格来说,pack configure
,尽管简单的 pack
假装你说的是 pack configure
)。
我的多项选择题显示有问题。我有一个框架,它显示了文本小部件上方的问题和选择,该小部件显示了所选答案是否正确。但是我想在测试未完成时隐藏文本。
它看起来像这样:
我的程序如下:
proc beginTest { number } {
startTest
.frq.txt configure -state normal
.frq.txt delete 1.0 end
set score 0
set save 0
set path Files/Tutorials/$::Tuto
set numbers { 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 }
for {set i 1} {$i<=10} {incr i} {
if { $::stop == 1 } {
set answer [tk_dialog .dialog "Quit test?" "If you quit the test now, your answers will be lost. Do you want to continue?" \
question 1 "Continue test" "Quit test"]
if {$answer == 0} {
continue
} else {
break
}
} else {
set ind [expr {int(rand()*[llength $numbers])} ]
incr score [displayQuestion $path [lindex $numbers $ind] $i 10]
set numbers [lreplace $numbers $ind $ind]
}
}
.frq.txt insert end "Your score: $score/10"
.frq.txt configure -state disabled
if {[info exist user]} {
if {[lindex $::testScores $number]== "Not Taken" || [lindex $::testScores $number] < $score} {
set ::testScores [lreplace $::testScores $number $number $score]
}
}
destroy .frq.fr2
}
在前一个程序的第一行调用的以下过程创建并定义 windows、框架、文本区域等
proc startTest {} {
destroy .fr
#window definition
toplevel .frq
wm title .frq "Test"
wm geometry .frq 740x670+0+0
.frq configure -bg $::bgColour
# title of section
label .frq.titl
.frq.titl configure -font "arial 20" -pady 10
grid .frq.titl -column 0 -row 0 -sticky swe
.frq.titl configure -background "White"
# define the main text zone
text .frq.txt -wrap word -yscroll {.frq.vscroll set} -highlightthickness 0
.frq.txt configure -background "White" ;#"slate gray" to debug
scrollbar .frq.vscroll -orient vertical -command {.frq.txt yview}
grid .frq.txt -column 0 -row 1 -sticky snwe
grid rowconfigure .frq 1 -weight 1
grid columnconfigure .frq 0 -weight 1
grid .frq.vscroll -column 1 -row 1 -sticky snwe
.frq.txt tag configure center -justify center
.frq.txt configure -font "times 12" -state disabled
.frq.vscroll configure -orient vertical -command {.frq.txt yview}
grid .frq.txt -column 0 -row 1 -sticky snwe
.frq.txt configure -state normal
.frq.txt delete 1.0 end
#text styles
.frq.txt tag configure Normal -font "times 12"
.frq.txt tag configure subTitle -font "times 14"
.frq.txt tag configure Titlec -font "times 16" -justify "center"
.frq.txt tag configure subTitlec -font "times 14" -justify "center"
.frq.txt tag configure subTitlecu -font "times 14" -justify "center" -underline on
.frq.txt tag configure Titlecu -font "times 16" -justify "center" -underline on
.frq.txt tag configure Title -font "times 16"
.frq.txt tag configure link -foreground blue -font "times 12"
.frq.txt tag configure right -foreground "forest green"
.frq.txt tag configure wrong -foreground red
.frq.txt tag configure enhance -background "light goldenrod"
.frq.txt tag configure rightenhance -background "light goldenrod" -foreground "forest green"
.frq.txt tag bind link <Enter> ".fr.txt configure -cursor hand1"
.frq.txt tag bind link <Leave> ".fr.txt configure -cursor arrow"
#questions frame
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
}
最后,显示问题的程序:
proc displayQuestion {path filename ind maxind} {
set fd [open $path/$filename r]
set questionFile [split [read $fd] "\n"]
set ::goOn 0
set answers [list]
set imgFlag 0
set fd [open $path/answers/$filename r]
set goodAnswer [read $fd]
close $fd
.frq.lbl configure -text "Question $ind of $maxind"
.frq.fr2.txt configure -state normal
.frq.fr2.txt delete 1.0 end
.frq.fr2.txt insert end \n
foreach line $questionFile {
if {$line == "<image>"} {
set imgFlag 1
continue
}
if {$imgFlag == 1} {
set imgFlag 0
.frq.fr2.txt insert end " " center
.frq.fr2.txt image create end -image [image create photo -file [file join "$path/$line"]]
continue
}
if {[string match A)* $line]} {
.frq.fr2.lblA configure -text $line
lappend answers $line
continue
}
if {[string match B)* $line]} {
.frq.fr2.lblB configure -text $line
lappend answers $line
continue
}
if {[string match C)* $line]} {
.frq.fr2.lblC configure -text $line
lappend answers $line
continue
}
if {[string match D)* $line]} {
.frq.fr2.lblD configure -text $line
lappend answers $line
continue
}
.frq.fr2.txt insert end $line\n center
}
.frq.fr2.txt configure -state disabled
.frq.txt insert end [lindex $questionFile 0]\n
.frq.fr2.cb1 deselect
.frq.fr2.cb2 deselect
.frq.fr2.cb3 deselect
.frq.fr2.cb4 deselect
.frq.bar.next configure -state disabled
set ::choice 0
vwait ::goOn
switch $::choice {
A { set ind 0 }
B { set ind 1 }
C { set ind 2 }
default { set ind 3 }
}
if { $::choice == $goodAnswer} {
.frq.txt insert end "Correct answer: [lindex $answers $ind]\n\n" right
return 1
} else {
switch $goodAnswer {
A { set i 0 }
B { set i 1 }
C { set i 2 }
default { set i 3 }
}
.frq.txt insert end "Wrong answer: [lindex $answers $ind]\n\tYou should have pick [lindex $answers $i]\n\n" wrong
return 0
}
}
相关的小部件是“.frq.txt”。 我尝试了 "pack" 和 "pack forget",甚至尝试了一种使用选项“-elide”的方法,但我没有想法。 你能帮帮我吗?
由于您使用 grid
来管理 .frq
小部件的内容,包括 .frq.txt
小部件,因此您需要使用 grid remove
或 grid forget
使小部件消失。两者的区别?使用 grid remove
,将记住单元格的大小调整元数据。
pack forget
用于撤消 pack
(严格来说,pack configure
,尽管简单的 pack
假装你说的是 pack configure
)。