[R] tcl/tk bind destroy event

Franco Mendolia franco.mendolia at gmx.de
Wed Aug 2 22:06:17 CEST 2006


Hello!

I want to create a messagebox whenever the user wants to destroy the window 
(e.g. <Alt-F4> or the 'x' in the right top corner) and ask if a modified file 
should be saved or not.

If 'cancel' is chosen then nothing should happen and the windows still should be 
existing. This doesn't work. When I press cancel the window will be destroyed 
although.

I also implemented a menu item 'Quit' where I show the same messagebox and there 
it works fine.

How can I make it work or is there another method to do this? I'm very new to R 
and tcl/tk.

Here is part of my code:


   exitProg <- function()
   {
     returnVal <- tkmessageBox(title="Question",
       message="Save modified file?",
       icon="question", type="yesnocancel", default="yes")

     returnVal <- as.character(returnVal)

     if( returnVal == "yes" )
     {
       # save file
       value <- saveFile()
       # destroy window when save was successfull
       if( value == 1 )
         tkdestroy(mw)
     }
     if( returnVal == "no" )
     {
       tkdestroy(mw)
     }
     if( returnVal == "cancel" )
     {
       # do nothing
       cat("Cancel was pressed.\n")
     }
   }

   # bind the destroy event in order to show a message box
   tkbind(mw,"<Destroy>",exitProg)

   # menu item which works fine
   tkadd(fileMenu, "command", label="Quit", command=exitProg)



Thank you.

Franco Mendolia



More information about the R-help mailing list