[R-gui] tkbutton() function call problem

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Tue Jul 3 20:06:44 CEST 2007


Robson, Samuel wrote:
> Dear all,
>
> I have run into a small problem with a GUI that I have been writing in R using the tcltk package, and I was hoping that somebody on this forum may be able to shed some light on it for me. The problem that I have is that when the main function is called by a tkbutton() widget, the GUI window goes a bit funny; all non-widget areas turn see-through. On top of this, a counter that should be updating constantly will only update if the GUI window is moved. It appears that the window is not loading fully, so even though the counter is increasing correctly, it does not update on the window until you manually force it by moving it. 
>
> However, if the function is called from the command line instead of linking it to a button press, everything works fine. This leads me to believe that the problem lies with the tkbutton(..., command=foo) command.
>
> As an example, consider the following code:
>
> tt <- tktoplevel()
> frameOverall <- tkframe(tt)
> counter <- tclVar("0")
>
> ## Top frame to contain counter
> topFrame <- tkframe(frameOverall)
>     progressLabel <- tklabel(topFrame, text=as.character(tclvalue(counter)))
>     tkgrid(tklabel(topFrame, text="	"))
>     tkgrid(progressLabel)
>     tkgrid(tklabel(topFrame, text="	"))
> tkgrid(topFrame)
>
> ## Function that makes the counter increase from 1 to 1000 when you press OK
> foo <- function() {
>     for (i in 1:1000) {
>         counter <- tclVar(as.character(as.numeric(tclvalue(counter))+1))
>         tkconfigure(progressLabel, textvariable=counter)
>     }	
> }
>
> ## Bottom frame to contain OK button
> botFrame <- tkframe(frameOverall)
>     tkgrid(tkbutton(botFrame, text="OK", command=foo))
> tkgrid(botFrame)
>
> tkgrid(frameOverall)
>
> Pressing the button should cause the counter to increase. However the problem mentioned above occurs (move the window about to see the counter change. You may also see the window turn see-through). Now run the script again, but instead of pressing OK, run foo() manually in the command line. Everything works fine.
>
> Is anybody able to tell me what is going wrong here? And more importantly, is there a way around it?
>
>   
It's not happening for me (SuSE Linux 10.2). I don't see anything until
the loop is finished, though, which is as it should be since nothing
allows the event loop to process events (a tcl("update","idletasks") in
the loop fixes that) .

The coding is a bit odd in that you regenerate the counter variable on
each iteration, which is not the Intended Way. Rather, it is

foo <- function() {
    counter <- tclVar(0)
    tkconfigure(progressLabel, textvariable=counter)

   for (i in 1:1000) {
        tclvalue(counter) <- as.character(as.numeric(tclvalue(counter))+1)
     }	
}






> Many thanks for your assistance,
>
> Sam Robson
> MOAC Doctoral Training Center
> Coventry House
> University of Warwick
> Coventry
> CV4 7AL
>
>
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-GUI mailing list
> R-SIG-GUI at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-gui
>   


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-SIG-GUI mailing list