Home > Mobile >  Python - How do I get the window handle from my OWN application
Python - How do I get the window handle from my OWN application

Time:10-15

I am using win32gui. With FindWindow and FindWindowEx I am able to get the handle of any window, resize, setpos etc. but how am I able to grab my own windows handle? I was not able to identify it using FindWindow and passing the class or title. Is there any other way?

In this example print will just print 0 (unrelated code removed)

import win32gui
import sys

class GUI(QMainWindow):
 def __init__(self, parent=None):
    
    super().__init__(parent)
    uic.loadUi('window.ui', self)



if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = GUI()
    win.show()

    mywindow = win32gui.FindWindow("QMainWindow",None)
    print ("My Window: ",mywindow)

    sys.exit(app.exec())

from my window.ui xml:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>My App</class>
<widget  name="QMainWindow">
[...]

EDIT: maybe winId()? But self.winId() and win.winId() return <PyQt6.sip.voidptr object at 0x000002B0[...]

CodePudding user response:

I got it

[...]
winhandle = int(win.winId())
print("My Window: ",winhandle)

Still curious why it didn't work with FindWindow
edit: found out that PyQt Standart Window Class is MainWindow , without the Q.

So mywindow = win32gui.FindWindow("MainWindow",None) without the Q works.

  • Related