[Rd] Selections from tcltk list boxes

John Fox jfox at mcmaster.ca
Fri Apr 1 23:49:48 CEST 2005


Dear r-devel list members,

I've experienced the following problem in getting a selection from a Tk list
box using tcltk. This is a long-standing problem, but I've finally decided
to tackle it.

Consider the following:

Library(tcltk)
top <- tktoplevel()
listbox <- tklistbox(top, height="10", width="2", exportselection="FALSE",
     selectmode="single")
for (x in letters[1:10]) tkinsert(listbox, "end", x)
value <- tclVar("")
entry <- tkentry(top, width="2", textvariable=value)
onOK <- function(){
    tkdestroy(top)
    print(tclvalue(value))
    return()
    }
onSelect <- function(){
    selection <- letters[1:10][as.numeric(tkcurselection(listbox)) + 1]
    tclvalue(value) <- selection
    }
OK <- tkbutton(top, text="OK", command=onOK)
tkgrid(listbox, entry, sticky="nw")
tkgrid(OK)
tkbind(listbox, "<ButtonPress-1>", onSelect)

As I understand it, single-clicking on an entry in the list box should
execute onSelect(), placing the selection in the entry box. What happens
instead is that one has to click twice on the entry, once (apparently) to
move the highlight to it and again to select it, executing onSelect().

If, however, I substitute tkbind(listbox, "<Double-ButtonPress-1>",
onSelect) for the last line, everything works as expected: double-clicking
on an entry executes onSelect(). One doesn't have to triple-click or
double-click twice.

Does anyone have an idea of what's going on, or how to get the behaviour
that I want -- i.e., to have a single click execute onSelect()?

I just checked all this on a Windows XP system under R 2.0.1 but have
observed the problem under Linux as well.

Thank you,
 John 

--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox



More information about the R-devel mailing list