The following examples illustrate how to use a list box in a Tk window. The first example does not have a scrollbar, so it is simpler.
require(tcltk)
tt<-tktoplevel()
tl<-tklistbox(tt,height=4,selectmode="single",background="white")
tkgrid(tklabel(tt,text="What's your favorite fruit?"))
tkgrid(tl)
fruits <- c("Apple","Orange","Banana","Pear")
for (i in (1:4))
{
tkinsert(tl,"end",fruits[i])
}
tkselection.set(tl,2) # Default fruit is Banana. Indexing starts at zero.
OnOK <- function()
{
fruitChoice <- fruits[as.numeric(tkcurselection(tl))+1]
tkdestroy(tt)
msg <- paste("Good choice! ",fruitChoice,"s are delicious!",sep="")
tkmessageBox(message=msg)
}
OK.but <-tkbutton(tt,text=" OK ",command=OnOK)
tkgrid(OK.but)
tkfocus(tt)
The code above produces the following window:


require(tcltk)
tt<-tktoplevel()
scr <- tkscrollbar(tt, repeatinterval=5,
command=function(...)tkyview(tl,...))
tl<-tklistbox(tt,height=4,selectmode="single",yscrollcommand=function(...)tkset(scr,...),background="white")
tkgrid(tklabel(tt,text="What's your favorite fruit?"))
tkgrid(tl,scr)
tkgrid.configure(scr,rowspan=4,sticky="nsw")
fruits <- c("Apple","Orange","Banana","Pear","Cherry","Apricot","Peach")
for (i in (1:7))
{
tkinsert(tl,"end",fruits[i])
}
tkselection.set(tl,2) # Default fruit is Banana. Indexing starts at zero.
OnOK <- function()
{
fruitChoice <- fruits[as.numeric(tkcurselection(tl))+1]
tkdestroy(tt)
msg <- paste("Good choice! ",fruitChoice,"s are delicious!",sep="")
tkmessageBox(message=msg)
}
OK.but <-tkbutton(tt,text=" OK ",command=OnOK)
tkgrid(OK.but)
tkfocus(tt)
The code above produces the following window:


require(tcltk)
tt<-tktoplevel()
tl<-tklistbox(tt,height=4,selectmode="single",background="white")
tkgrid(tklabel(tt,text="Please delete the fruit(s) which you don't like."))
tkgrid(tl)
fruits <- c("Apple","Orange","Banana","Pear")
for (i in (1:4))
{
tkinsert(tl,"end",fruits[i])
}
tkselection.set(tl,2) # Default fruit is Banana. Indexing starts at zero.
OK.but <-tkbutton(tt,text=" OK ",command=function() tkdestroy(tt))
DeleteSelection <- function()
{
fruitIndex <- as.integer(tkcurselection(tl))
tkdelete(tl,fruitIndex)
}
DeleteSelection.but <- tkbutton(tt,text="Delete",command=DeleteSelection)
tkgrid(DeleteSelection.but)
tkgrid(tklabel(tt,text=" "))
tkgrid(OK.but)
tkfocus(tt)
Running the code above gives the following window:
We will delete "Orange" from the list
The window below shows that "Orange" has now been deleted from the list.