如何将 PyQt QProcess window 置于最前面?
How to bring PyQt QProcess window to front?
我正在启动一个 QProcess,并且希望在每次向 QProcess 写入命令时将结果 window 置于顶部。
但是,如果我的进程是 "gnuplot.exe" (Win7),绘图 window 将更新,但始终落后于 PyQt window。我还没有找到一种方法来将它带到前面,或者让它活跃起来,或者把重点放在上面,或者你怎么称呼它。
就像是
self.process。 ...
raise(), show(), activateWindow(), SetForegroundWindow, WindowStaysOnTopHint ... ???
这些帖子对我没有进一步帮助
How to find the active PyQt window and bring it to the front
https://forum.qt.io/topic/30018/solved-bring-to-front-window-application-managed-with-qprocess/3
Bring QProcess window to front (running Qt Assistant)
代码如下:
import sys
from PyQt5.QtCore import QProcess, pyqtSlot
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
import subprocess
class MyWindow(QWidget):
def __init__(self):
super(MyWindow,self).__init__()
self.setGeometry(100,100,500,500)
self.button1 = QPushButton('New plot',self)
self.button1.clicked.connect(self.button1_clicked)
self.process = QProcess(self)
self.process.start(r'C:\Programs\gnuplot\bin\gnuplot.exe', ["-p"])
self.n = 1
def button1_clicked(self):
plotcmd = "plot x**"+str(self.n)+"\n"
self.process.write(plotcmd.encode())
self.n += 1
response = self.process.readAllStandardOutput()
print(str(response, encoding="utf-8"))
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle("plastique")
window = MyWindow()
window.show()
sys.exit(app.exec_())
根据@buck54321 的评论和链接,我得到了以下似乎(几乎)有效的版本。
上面链接中的解决方案似乎更通用,对我的情况来说有点太复杂,因为我知道我想放在前面的 window 的确切名称。
对于我的原始代码,我只需添加三行。我对win32gui不熟悉。
然而,使用我的代码,只有在第二次按下按钮后,window 才会被带到前面。在第一次按下按钮后,hwnd 仍然是 0,这会导致崩溃。我猜这是因为 gnuplot.exe 将在不打开 window 的情况下启动,并且仅在发送第一个命令后才打开 window。
如果有人能告诉我如何在单击第一个按钮后将 window 置于最前面,那就太好了。
如果有任何限制或改进,我很乐意了解它们。
代码如下:
import sys
from PyQt5.QtCore import QProcess, pyqtSlot
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
import subprocess
import win32gui ####### new line
class MyWindow(QWidget):
def __init__(self):
super(MyWindow,self).__init__()
self.setGeometry(100,100,500,500)
self.button1 = QPushButton('New plot',self)
self.button1.clicked.connect(self.button1_clicked)
self.process = QProcess(self)
self.process.start(r'C:\Programs\gnuplot\bin\gnuplot.exe', ["-p"])
self.n = 1
def button1_clicked(self):
plotcmd = "set term wxt 999\n plot x**"+str(self.n)+"\n"
self.process.write(plotcmd.encode())
self.n += 1
hwnd = win32gui.FindWindow(None, "Gnuplot (window id : 999)") ####### new line
response = self.process.readAllStandardOutput()
print(str(response, encoding="utf-8"))
if hwnd != 0: win32gui.SetForegroundWindow(hwnd) ####### new line
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle("plastique")
window = MyWindow()
window.show()
sys.exit(app.exec_())
我正在启动一个 QProcess,并且希望在每次向 QProcess 写入命令时将结果 window 置于顶部。
但是,如果我的进程是 "gnuplot.exe" (Win7),绘图 window 将更新,但始终落后于 PyQt window。我还没有找到一种方法来将它带到前面,或者让它活跃起来,或者把重点放在上面,或者你怎么称呼它。 就像是 self.process。 ... raise(), show(), activateWindow(), SetForegroundWindow, WindowStaysOnTopHint ... ???
这些帖子对我没有进一步帮助
How to find the active PyQt window and bring it to the front
https://forum.qt.io/topic/30018/solved-bring-to-front-window-application-managed-with-qprocess/3
Bring QProcess window to front (running Qt Assistant)
代码如下:
import sys
from PyQt5.QtCore import QProcess, pyqtSlot
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
import subprocess
class MyWindow(QWidget):
def __init__(self):
super(MyWindow,self).__init__()
self.setGeometry(100,100,500,500)
self.button1 = QPushButton('New plot',self)
self.button1.clicked.connect(self.button1_clicked)
self.process = QProcess(self)
self.process.start(r'C:\Programs\gnuplot\bin\gnuplot.exe', ["-p"])
self.n = 1
def button1_clicked(self):
plotcmd = "plot x**"+str(self.n)+"\n"
self.process.write(plotcmd.encode())
self.n += 1
response = self.process.readAllStandardOutput()
print(str(response, encoding="utf-8"))
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle("plastique")
window = MyWindow()
window.show()
sys.exit(app.exec_())
根据@buck54321 的评论和链接,我得到了以下似乎(几乎)有效的版本。 上面链接中的解决方案似乎更通用,对我的情况来说有点太复杂,因为我知道我想放在前面的 window 的确切名称。 对于我的原始代码,我只需添加三行。我对win32gui不熟悉。
然而,使用我的代码,只有在第二次按下按钮后,window 才会被带到前面。在第一次按下按钮后,hwnd 仍然是 0,这会导致崩溃。我猜这是因为 gnuplot.exe 将在不打开 window 的情况下启动,并且仅在发送第一个命令后才打开 window。 如果有人能告诉我如何在单击第一个按钮后将 window 置于最前面,那就太好了。
如果有任何限制或改进,我很乐意了解它们。
代码如下:
import sys
from PyQt5.QtCore import QProcess, pyqtSlot
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
import subprocess
import win32gui ####### new line
class MyWindow(QWidget):
def __init__(self):
super(MyWindow,self).__init__()
self.setGeometry(100,100,500,500)
self.button1 = QPushButton('New plot',self)
self.button1.clicked.connect(self.button1_clicked)
self.process = QProcess(self)
self.process.start(r'C:\Programs\gnuplot\bin\gnuplot.exe', ["-p"])
self.n = 1
def button1_clicked(self):
plotcmd = "set term wxt 999\n plot x**"+str(self.n)+"\n"
self.process.write(plotcmd.encode())
self.n += 1
hwnd = win32gui.FindWindow(None, "Gnuplot (window id : 999)") ####### new line
response = self.process.readAllStandardOutput()
print(str(response, encoding="utf-8"))
if hwnd != 0: win32gui.SetForegroundWindow(hwnd) ####### new line
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyle("plastique")
window = MyWindow()
window.show()
sys.exit(app.exec_())