Skip to content

Instantly share code, notes, and snippets.

@justinfx
Created March 1, 2012 18:00
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save justinfx/1951709 to your computer and use it in GitHub Desktop.
Save justinfx/1951709 to your computer and use it in GitHub Desktop.
Example of how to use PyQt to popup a dialog at current mouse pos, and close in response to ESC keyc
from PyQt4 import QtCore, QtGui
class Dialog(QtGui.QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.resize(300,200)
def showEvent(self, event):
geom = self.frameGeometry()
geom.moveCenter(QtGui.QCursor.pos())
self.setGeometry(geom)
super(Dialog, self).showEvent(event)
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.hide()
event.accept()
else:
super(Dialog, self).keyPressEvent(event)
if __name__ == "__main__":
app = QtGui.QApplication([])
d = Dialog()
d.show()
d.raise_()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment