rrstudiotcltk

How to move a tcl/tk window you didn't construct yourself to toplevel?


I'm running

test_directory <- tcltk::tk_choose.dir()

(from the tcltk package) which works and does what I want, but it consistently appears hidden behind RStudio. (tkchoosedirectory() seems to behave the same way.)

Is there a way to make the selection window move to the front/toplevel?
or perhaps more generally...
How do you move a tcl/tk window you didn't construct yourself to toplevel?

Found this and this thread but can't make sense of how I would implement the different solutions to my example. They assume you have built your own window and therefore have access to objects in the environment created along the way, which isn't the case here.

From looking at the documentation example for tk_choose.dir() I tried this:

tt <- tktoplevel()
test_directory <- tcltk::tk_choose.dir()

Where:

Nesting tk_choose.dir() in something probably won't work as it returns a string with the path. What's left would be changing some kind of default setting, either globally or within the function.

Desired result is that when I run a custom function, asking the user to select a file or directory, the window isn't hidden. I plan to use this in a custom package but I figure if it works in a function in a script I should be able to make that work too.
A beginner level explanation so I can understand how this works would be nice, if possible.

edit:
This thread is probably the closest there is to an answer then.


Solution

  • This thread is probably the closest we'll get to an answer.

    The following seems to have the desired result sometimes.

    root <- tktoplevel()
    tkraise(root)
    # Sys.sleep(1)
    test_dir <- tclvalue(tkchooseDirectory(initialdir=getwd()))
    tkdestroy(root)
    

    If there's a better solution that actually works please post that as well.