QT window closes immediately

M

Martin Caum

I am attempting to open a window on mouse activity which works, but
the window fails to stay open.
I set it to terminate when the escape key is pressed even when the
program is not currently selected. This works fine. Originally I had
it create the window only with a right click, but when I noticed the
window closed immediately I set it to any mouse activity for easier
debugging. My script is as follows:
Code:
import pythoncom, pyHook
import sys
import win32api
from PyQt4 import QtGui, QtCore

class WindowObject(QtGui.QWidget):
	def __init__(self, parent=None):
		QtGui.QWidget.__init__(self, parent)
		self.setWindowTitle('Window')
		self.resize(50, 250)

def OnKeyboardEvent(event):
	if event.KeyID == 27:
		win32api.PostQuitMessage()
	return 1

def OnMouseEvent(event):
	x = event.Position[0]
	y = event.Position[1]
	createWindow(x, y)
	return 1

def createWindow(x, y):
	menu = WindowObject()
	menu.move(x, y)
	menu.show()

hm = pyHook.HookManager()
hm.MouseAll = OnMouseEvent
hm.KeyDown = OnKeyboardEvent
hm.HookMouse()
hm.HookKeyboard()

app = QtGui.QApplication(sys.argv)
pythoncom.PumpMessages()
I'm fairly certain that this is due to my lack of understanding in the
PumpMessages command and the HookManager. Any advice?
 
D

Diez B. Roggisch

Martin Caum said:
I am attempting to open a window on mouse activity which works, but
the window fails to stay open.
I set it to terminate when the escape key is pressed even when the
program is not currently selected. This works fine. Originally I had
it create the window only with a right click, but when I noticed the
window closed immediately I set it to any mouse activity for easier
debugging. My script is as follows:
Code:
import pythoncom, pyHook
import sys
import win32api
from PyQt4 import QtGui, QtCore

class WindowObject(QtGui.QWidget):
	def __init__(self, parent=None):
		QtGui.QWidget.__init__(self, parent)
		self.setWindowTitle('Window')
		self.resize(50, 250)

def OnKeyboardEvent(event):
	if event.KeyID == 27:
		win32api.PostQuitMessage()
	return 1

def OnMouseEvent(event):
	x = event.Position[0]
	y = event.Position[1]
	createWindow(x, y)
	return 1

def createWindow(x, y):
	menu = WindowObject()
	menu.move(x, y)
	menu.show()

hm = pyHook.HookManager()
hm.MouseAll = OnMouseEvent
hm.KeyDown = OnKeyboardEvent
hm.HookMouse()
hm.HookKeyboard()

app = QtGui.QApplication(sys.argv)
pythoncom.PumpMessages()
I'm fairly certain that this is due to my lack of understanding in the
PumpMessages command and the HookManager. Any advice?

1) Keep a reference to your window-object. I can only guess (PyQt not
being a regular topic when hacking for me.. .sadly), but GC can
interfere with these kind of things.

2) what is the win32 stuff about? Qt should abstract from that.

3) if in doubt, as on the PyQt mailing list.

Diez
 
N

News123

I am attempting to open a window on mouse activity which works, but
the window fails to stay open.
I set it to terminate when the escape key is pressed even when the
program is not currently selected. This works fine. Originally I had
it create the window only with a right click, but when I noticed the
window closed immediately I set it to any mouse activity for easier
debugging. My script is as follows:
Code:
import pythoncom, pyHook
import sys
import win32api
from PyQt4 import QtGui, QtCore

class WindowObject(QtGui.QWidget):
	def __init__(self, parent=None):
		QtGui.QWidget.__init__(self, parent)
		self.setWindowTitle('Window')
		self.resize(50, 250)

def OnKeyboardEvent(event):
	if event.KeyID == 27:
		win32api.PostQuitMessage()
	return 1

def OnMouseEvent(event):
	x = event.Position[0]
	y = event.Position[1]
	createWindow(x, y)
	return 1

def createWindow(x, y):
	menu = WindowObject()
	menu.move(x, y)
	menu.show()

hm = pyHook.HookManager()
hm.MouseAll = OnMouseEvent
hm.KeyDown = OnKeyboardEvent
hm.HookMouse()
hm.HookKeyboard()

app = QtGui.QApplication(sys.argv)
pythoncom.PumpMessages()
I'm fairly certain that this is due to my lack of understanding in the
PumpMessages command and the HookManager. Any advice?


Shouldn't every QT application have an event loop.

I would have expecte a call to app._exec()

as you need already pumpMessages() fro the COM interface, I assume you
had to use the threading or multiprocessing module as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top