How can I open the R Tk window on the front panel after launching via Rscript from another application?

I have a script line by line:

if (!require(tcltk2)) {install.packages('tcltk2', repos=""); require(tcltk2)}

base <- NULL
done <- tclVar(0)

quasitelgui <- function(inputfile = NULL)
    base <- tktoplevel()
    tkwm.title(base, "QuasiTel")

    # Files
    file.frm <- tkframe(base, borderwidth=2)
    datafile.lbl <- tklabel(file.frm, text="Data")
    datafile.entry <- tkentry(file.frm, state="readonly")
    datafile.btn <- tkbutton(file.frm, text="Browse...")
    tkgrid(datafile.lbl, datafile.entry, datafile.btn)
    tkgrid.configure(datafile.lbl, sticky="e")
    tkgrid.configure(datafile.entry, sticky="ew", padx=1)
    tkgrid.columnconfigure(file.frm, 1, weight=1)
    tkgrid.configure(file.frm, sticky="ew")

    # Main
    main.frm <- tkframe(base, borderwidth=2)
    g1.lbl <- tklabel(main.frm, text="Group 1")
    g2.lbl <- tklabel(main.frm, text="Group 2")
    tkgrid(g1.lbl, g2.lbl)

    q.btn <- tkbutton(bott.frm, text="Quit", command=function() tclvalue(done) <- 1)
    tkbind(base,"<Destroy>", function() tclvalue(done) <- 2)
    tkgrid(filter.lbl, columnspan=2)
    tkgrid(ok.btn, q.btn)
    tkgrid.configure(ok.btn, q.btn, padx=1)
    tkgrid.columnconfigure(base, 0, weight=1)

    if (length(inputfile) > 0) { }

cmd.args <- commandArgs(trailingOnly=TRUE)

if (length(cmd.args) > 0) {
    quasitelgui(gsub("\\\\", "/", cmd.args[1]))
} else {



I am running it via rscript from another GUI. I want the window to grab focus when it starts. Tkfocus doesn't do this.


source to share

3 answers

Not focusing, but raising:

> library(tcltk)
Loading Tcl/Tk interface ... done
> w1 <- tktoplevel()
> w2 <- tktoplevel()
> tkraise(w1)




I think jverzani is correct in that many, if not all (modern) GUI systems (I mean OS / desktop level, not GUI tools) a href = " / wiki / Focus_stealing "rel =" nofollow noreferrer "> stealing focus. A new process that wants to grab focus is a perfect case of attempting to steal focus, so I tend to think that if your script, Matt, is running in a different process, you can't expect it to actually grab focus. There are system-dependent ways to draw the user's attention to a particular window, but I doubt they are directly supported by Tk.



On MS Windows, you can follow like this:

info_sys <- # sniff the O.S.

if (info_sys['sysname'] == 'Windows') { # MS Windows trick
 shell("powershell -command [void] [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') ; [Microsoft.VisualBasic.Interaction]::AppActivate('The title of your window') ")




All Articles