启动定时器检测记事本的位置大小和是否关闭
self.tmpHwnd = None
self.checkTimer = QTimer(self, timeout=self.checkWindow)
self.checkTimer.start(10) # 10毫秒比较流畅
def checkWindow(self):
# 查找
self.hwnd = win32gui.FindWindow('WTWindow', '窗口名字')
if win32gui.GetForegroundWindow() == self.hwnd:
if Demo.isVisible() == False:
Demo.setVisible(True)
else:
print(win32gui.GetForegroundWindow())
if Demo.isVisible() == True:
Demo.setVisible(False)
if self.tmpHwnd and not self.hwnd:
# 表示父窗口被关闭了
self.checkTimer.stop()
self.close() # 关闭自己
return
if not self.hwnd:
return
self.tmpHwnd = self.hwnd
# 获取位置
rect = win32gui.GetWindowRect(self.hwnd)
self.move(rect[0]+300, rect[1]+20)