| 22 | |
| 23 | |
| 24 | class Window(QWidget): |
| 25 | |
| 26 | def __init__(self, *args, **kwargs): |
| 27 | super(Window, self).__init__(*args, **kwargs) |
| 28 | layout = QVBoxLayout(self) |
| 29 | layout.addWidget(QPushButton('test', self)) |
| 30 | self.tmpHwnd = None |
| 31 | # 启动定时器检测记事本的位置大小和是否关闭 |
| 32 | self.checkTimer = QTimer(self, timeout=self.checkWindow) |
| 33 | self.checkTimer.start(10) # 10毫秒比较流畅 |
| 34 | |
| 35 | def checkWindow(self): |
| 36 | # 查找 |
| 37 | hwnd = win32gui.FindWindow('Notepad', None) |
| 38 | if self.tmpHwnd and not hwnd: |
| 39 | # 表示记事本关闭了 |
| 40 | self.checkTimer.stop() |
| 41 | self.close() # 关闭自己 |
| 42 | return |
| 43 | if not hwnd: |
| 44 | return |
| 45 | self.tmpHwnd = hwnd |
| 46 | # 获取位置 |
| 47 | rect = win32gui.GetWindowRect(hwnd) |
| 48 | print(rect) |
| 49 | self.move(rect[2], rect[1]) |
| 50 | |
| 51 | |
| 52 | if __name__ == '__main__': |