问题描述
所以我发现了这个例子,它真的很酷,我想在一个应用程序中做一些类似的事情。我发现的问题是,是的,您可以嵌入窗口,是的,您可以单击某些内容,但无法在窗口中键入内容。例如,如果我打开一个 chrome 或 edge 窗口并选择将其嵌入,我将无法在嵌入的浏览器中输入内容。
from PyQt5.QtGui import QWindow
from PyQt5.QtWidgets import QWidget,QVBoxLayout,QPushButton,QListWidget,QLabel
import win32con
import win32gui
class Window(QWidget):
def __init__(self,*args,**kwargs):
super(Window,self).__init__(*args,**kwargs)
self.resize(800,600)
layout = QVBoxLayout(self)
self.myhwnd = int(self.winId())
layout.addWidget(QPushButton('get windows',self,clicked=self._getwindowList,maximumHeight=30))
layout.addWidget(QLabel('load',maximumHeight=30))
self.windowList = QListWidget(self,itemDoubleClicked=self.onItemDoubleClicked,maximumHeight=200)
layout.addWidget(self.windowList)
def closeEvent(self,event):
if self.layout().count() == 4:
self.restore()
super(Window,self).closeEvent(event)
def _getwindowList(self):
self.windowList.clear()
win32gui.EnumWindows(self._enumWindows,None)
def onItemDoubleClicked(self,item):
self.windowList.takeItem(self.windowList.indexFromItem(item).row())
hwnd,phwnd,_,_ = item.text().split('|')
if self.layout().count() == 4:
self.restore()
hwnd,phwnd = int(hwnd),int(phwnd)
style = win32gui.getwindowlong(hwnd,win32con.GWL_STYLE)
exstyle = win32gui.getwindowlong(hwnd,win32con.GWL_EXSTYLE)
print('save',hwnd,style,exstyle)
widget = QWidget.createWindowContainer(QWindow.fromWinId(hwnd))
widget.hwnd = hwnd
widget.phwnd = phwnd
widget.style = style
widget.exstyle = exstyle
self.layout().addWidget(widget)
def restore(self):
widget = self.layout().itemAt(3).widget()
print('restore',widget.hwnd,widget.style,widget.exstyle)
win32gui.SetParent(widget.hwnd,widget.phwnd)
win32gui.SetwindowLong( widget.hwnd,win32con.GWL_STYLE,widget.style | win32con.WS_VISIBLE)
win32gui.SetwindowLong( widget.hwnd,win32con.GWL_EXSTYLE,widget.exstyle)
win32gui.ShowWindow( widget.hwnd,win32con.SW_SHOW)
widget.close()
self.layout().removeWidget(widget)
widget.deleteLater()
def _enumWindows(self,_):
if hwnd == self.myhwnd:
return
if win32gui.IsWindow(hwnd) and win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
phwnd = win32gui.GetParent(hwnd)
title = win32gui.GetwindowText(hwnd)
name = win32gui.GetClassName(hwnd)
self.windowList.addItem('{0}|{1}|\t{2}\t|\t{3}'.format(hwnd,title,name))
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
现在我的问题是如何将焦点更改为键盘的嵌入式窗口?事实上,当鼠标在窗口中时,键盘仍然聚焦在父窗口中。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)