Hello
I have a dialog (pasted below) that includes a couple of long listboxes
(require scrollbars). I want the display of the list to start at the
top, but by default the displayed portion of the list starts at the
selected position + 1 (or the first selected position +1 if extended
selection). This appears to be because the underlying tcl/tk indexing
begins at 0 while the tk2listbox indexing for selection starts (more
intuitively) at 1. However the tk2listbox function sets the selection
pointer using tksee with an unadjusted selection value, hence the
offset. This never matters for listboxes that are fully displayed, so
maybe slipped through the cracks. But for scrolled listboxes it means
the displayed list items do not include the selected (or first selected)
value, which is irksome. The dialog below resolves the problem with two
tksee commands, currently commented out to illustrate the problem with
the default behavior. The bit of code in the tk2listbox function...
for (item in values) tkinsert(w, "end", item)
if (!is.null(selection)) {
for (sel in selection) tkselection.set(w, sel - 1)
tksee(w, selection[1])
}
...should perhaps be...
for (item in values) tkinsert(w, "end", item)
if (!is.null(selection)) {
for (sel in selection) tkselection.set(w, sel - 1)
tksee(w, selection[1]-1)
}
I checked this revision with the SSGUI function below and it worked fine
without adding the tksee lines. Staying with the original tk2listbox
function with tksee fixes as below regardless, too much potential for
confusion down the road.
SSGUI=function() {
DiagnosticsR=tktoplevel()
tktitle(DiagnosticsR) <- "Survey Data Analysis with R"
tkgrid(tklabel(DiagnosticsR,text="DATA SELECTION &
DEFINITION"),stick="we")
tkgrid(tklabel(DiagnosticsR,text=""))
tkgrid(tklabel(DiagnosticsR,text="Survey"),tklabel(DiagnosticsR,text="St
ock"),tklabel(DiagnosticsR,text="Subset
Strata"),tklabel(DiagnosticsR,text="Sex"),sticky="n")
Surveys <- c("SUMMER ", "GEORGES","4VWCOD
","SPRING ","FALL ","GULF
")
Surveylist=tk2listbox(DiagnosticsR, values=Surveys, selection=1,
selectmode = "single", height = 6, scroll = "none",
autoscroll = "x",
enabled = TRUE)
Stocks <-
c("CAPELIN","COD","COD4VN","COD4VSW","COD4X","CUSK","DOGFISH","HADDOCK",
"HADDOCK4VW","HADDOCK4X","HALIBUT","HERRING","LITTLESKATE","LONGHORNSCUL
PIN",
"MONKFISH","PLAICE","PLAICE4VW","PLAICE4X","POLLOCK","POUT","REDFISH","R
EDFISHUNIT2","REDFISHUNIT3","ROSEFISH",
"SANDLANCE","SHANNY","SILVERHAKE","SMOOTHSKATE","SHORTFINSQUID","THORNYS
KATE","TURBOT",
"WHITEHAKE","WHITEHAKE4VN","WHITEHAKE4VSW","WHITEHAKE4X","WINTERFLOUNDER
","WINTERFLOUNDER4X","WINTERSKATE","WITCH","WITCH4VW","WITCH4X","WOLFFIS
H",
"YELLOWTAIL","YELLOWTAIL4VW","YELLOWTAIL4X",
"COD4T","HADDOCK4T","WHITEHAKE4T","SILVERHAKE4T","POLLOCK4T","REDFISH4T"
,"HALIBUT4T","PLAICE4T","WITCH4T","YELLOWTAIL4T","WINTERFLOUNDER4T","WOL
FFISH4T",
"THORNYSKATE4T","SMOOTHSKATE4T","LITTLESKATE4T","WINTERSKATE4T","DOGFISH
4T","MONKFISH4T","POUT4T")
Stocklist=tk2listbox(DiagnosticsR, values=Stocks, selection=2,
selectmode = "single", height = 20, scroll = "y", autoscroll
= "none",
enabled = TRUE)
#tksee(Stocklist,0)
Strata <-
as.character(c(seq(401,411),seq(415,429),seq(431,466),seq(470,478),seq(4
80,485),seq(490,495),seq(501,508)))
Stratalist=tk2listbox(DiagnosticsR, values=Strata, selection=seq(1,91),
selectmode = "extended", height = 20, scroll = "y",
autoscroll = "none",
enabled = TRUE)
#tksee(Stratalist,0)
Sex <- c("Combined", "Males","Females")
Sexlist=tk2listbox(DiagnosticsR, values=Sex, selection=1, selectmode
"single", height = 3, scroll = "none", autoscroll =
"y", enabled = TRUE)
tkgrid(Surveylist,Stocklist,Stratalist,Sexlist,sticky="n")
DoWB <- tk2checkbutton(DiagnosticsR, text = "Do Weights and
Biomass")
tkgrid(DoWB,sticky="w")
keepDoWB=tclVar("1")
tkconfigure(DoWB,variable=keepDoWB)
DoNA <- tk2checkbutton(DiagnosticsR, text = "Do Numbers and
Abundance")
tkgrid(DoNA,sticky="w")
keepDoNA=tclVar("1")
tkconfigure(DoNA,variable=keepDoNA)
DoGroups <- tk2checkbutton(DiagnosticsR, text = "Do Length Groups
(Numbers Only)")
tkgrid(DoGroups,sticky="w")
keepDoGroups=tclVar("0")
tkconfigure(DoGroups,variable=keepDoGroups)
G1small <- tclVar("1")
G1smallEntry=tk2entry(DiagnosticsR, textvariable=G1small)
tkgrid(tklabel(DiagnosticsR,text="Smallest (cm)"),G1smallEntry)
G1large <- tclVar("30")
G1largeEntry=tk2entry(DiagnosticsR, textvariable=G1large)
tkgrid(tklabel(DiagnosticsR,text="Largest (cm)"),G1largeEntry)
G2small <- tclVar("31")
G2smallEntry=tk2entry(DiagnosticsR, textvariable=G2small)
tkgrid(tklabel(DiagnosticsR,text="Smallest (cm)"),G2smallEntry)
G2large <- tclVar("150")
G2largeEntry=tk2entry(DiagnosticsR, textvariable=G2large)
tkgrid(tklabel(DiagnosticsR,text="Largest (cm)"),G2largeEntry)
DoCatchability <- tk2checkbutton(DiagnosticsR, text = "Use Documented
Transformations for Vessel Catchability Coefficients")
tkgrid(DoCatchability,sticky="w")
keepDoCatchability=tclVar("0")
tkconfigure(DoCatchability,variable=keepDoCatchability)
AppApply <- tk2button(DiagnosticsR, text = "Apply", width = 7,
command function()
ExtractDB(tclvalue(tclVar(Surveys[as.numeric(tkcurselection(Surveylist))
+1])),
tclvalue(tclVar(Stocks[as.numeric(tkcurselection(Stocklist))+1])),tclval
ue(tclVar(Strata[as.numeric(tkcurselection(Stratalist))+1])),
tclvalue(tclVar(Sex[as.numeric(tkcurselection(Sexlist))+1])),state(DoWB)
,state(DoNA),state(DoGroups),tclvalue(G1small),tclvalue(G1large),tclvalu
e(G2small),tclvalue(G2large),state(DoCatchability)))
AppCancel <- tk2button(DiagnosticsR, text = "Cancel", width = 7,
command
= function() tkdestroy(DiagnosticsR))
tkgrid(AppApply,sticky="w")
tkgrid(AppCancel,sticky="w")
#determine the window size (cover different screen sizes)
tkwm.geometry(DiagnosticsR,"")
tkfocus(DiagnosticsR)
}
Mark Fowler
Population Ecology Division
Bedford Inst of Oceanography
Dept Fisheries & Oceans
Dartmouth NS Canada
B2Y 4A2
Tel. (902) 426-3529
Fax (902) 426-9710
Email Mark.Fowler@dfo-mpo.gc.ca <mailto:Mark.Fowler@dfo-mpo.gc.ca>
[[alternative HTML version deleted]]