如何在从另一个应用程序通过Rscript启动后将一个R Tk窗口带到前端?

时间:2021-03-31 07:24:31

I have a script along the lines of:

我的剧本是这样写的:

if (!require(tcltk2)) {install.packages('tcltk2', repos="http://cran.us.r-project.org"); 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(file.frm)
    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(filter.entry)
    tkgrid(ok.btn, q.btn)
    tkgrid.configure(ok.btn, q.btn, padx=1)
    tkgrid(bott.frm)
    tkgrid.columnconfigure(base, 0, weight=1)

    if (length(inputfile) > 0) { datafile.open(inputfile) }
}

cmd.args <- commandArgs(trailingOnly=TRUE)

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

tkfocus(base)
tkwait.variable(done)
tkdestroy(base)

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

我用另一个GUI的rscript运行它。我想让窗口在开始时抓住焦点。Tkfocus不做。

3 个解决方案

#1


4  

Not focus, but raise:

不是重点,而是提高:

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

#2


0  

I think jverzani is correct is that many if not all (contemporary) GUI systems (I mean, OS/desktop level, not GUI toolkits) prevent focus stealing. A new process which wants to grab focus is a perfect case of a focus stealing attempt, so I'm inclined to think that if your script, Matt, runs in another process, you can't expect it to actually grab focus. There are system-dependent ways to draw user attention to a particular window but I doubt they are directly supported by Tk.

我认为jverzani是对的,许多(如果不是全部的话)GUI系统(我的意思是操作系统/桌面级,而不是GUI工具包)可以防止焦点窃取。一个想要获取焦点的新进程是一个偷取焦点的完美例子,所以我倾向于认为,如果您的脚本Matt在另一个进程中运行,那么您不能期望它实际上获取焦点。有一些系统依赖的方法可以将用户的注意力吸引到特定的窗口,但是我怀疑它们是否直接由Tk支持。

#3


0  

In MS Windows you can follow this way:

在MS Windows中,您可以遵循以下方式:

info_sys <- Sys.info() # 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') ")
}

#1


4  

Not focus, but raise:

不是重点,而是提高:

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

#2


0  

I think jverzani is correct is that many if not all (contemporary) GUI systems (I mean, OS/desktop level, not GUI toolkits) prevent focus stealing. A new process which wants to grab focus is a perfect case of a focus stealing attempt, so I'm inclined to think that if your script, Matt, runs in another process, you can't expect it to actually grab focus. There are system-dependent ways to draw user attention to a particular window but I doubt they are directly supported by Tk.

我认为jverzani是对的,许多(如果不是全部的话)GUI系统(我的意思是操作系统/桌面级,而不是GUI工具包)可以防止焦点窃取。一个想要获取焦点的新进程是一个偷取焦点的完美例子,所以我倾向于认为,如果您的脚本Matt在另一个进程中运行,那么您不能期望它实际上获取焦点。有一些系统依赖的方法可以将用户的注意力吸引到特定的窗口,但是我怀疑它们是否直接由Tk支持。

#3


0  

In MS Windows you can follow this way:

在MS Windows中,您可以遵循以下方式:

info_sys <- Sys.info() # 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') ")
}