【问题标题】:Setting focus of cursor back to Python将光标的焦点设置回 Python
【发布时间】:2018-11-26 17:42:20
【问题描述】:

我有一个使用 TKinter 打开 csv 的小程序。 一切正常。

当用户选择一个文件时,我希望光标和活动窗口返回到 Python shell。

我正在使用这个:

os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')

在 IDLE 中,这有效,当程序运行时,但是当我只是双击 .py 文件并在 Python Shell 中运行它时,它说它找不到路径。

有人知道我需要的路径吗?

谢谢,

【问题讨论】:

    标签: python tkinter os.system


    【解决方案1】:

    进一步研究,这是我的解决方案。

    import win32gui as wg
    from win32gui import GetWindowText, GetForegroundWindow
    
    #This gets the details of the current window, the one running the program
    aw = (GetForegroundWindow())
    
    #Do some stuff..
    
    #This tells the program to set the focus on the captured window
    wg.SetForegroundWindow(aw)
    

    我希望这可以帮助其他寻找与我相同的人。 :-)

    【讨论】:

      【解决方案2】:

      试试这个,它通过 pid 引用正在运行的进程,所以你如何运行它并不重要:

      import os
      pid = os.getpid()
      os.system("""/usr/bin/osascript -e 'tell application "System Events" to set frontmost of (first process whose unix id is %d) to true'""" % pid)
      

      【讨论】:

      • 嗨,谢谢。我得到相同的结果。关闭“选择文件”窗口后,光标不在任何窗口中,Python shell 返回“无法定位路径”错误
      • 我可能会看看我是否可以让 win32gui 工作,但我似乎无法安装它。
      • 哦,我假设您使用的是 Mac,因为您正在尝试纯 Mac 特定的解决方案。
      • 命令是基于 Unix 的,Powershell ion windows 将支持其中的大部分,这就是为什么我以前的“Finder”代码可以正常工作的原因。我想这个问题不是很好用它,因为它对 mac 来说有点多。我让 Win32Gui 工作。不过还是谢谢你的帮助。
      猜你喜欢
      • 2014-04-27
      • 2014-04-24
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 2018-08-07
      相关资源
      最近更新 更多