T
T
TomasHuk2017-10-14 22:12:13
Python
TomasHuk, 2017-10-14 22:12:13

How to close a PyQt window with a hotkey combination?

Let's say we have a normal window:

import sys
from PyQt4 import QtGui, QtCore

class Wind(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint|QtCore.Qt.Tool)
        self.setGeometry(300, 300, 250, 150)

    def close_window(self):
        self.close()
        quit()

app = QtGui.QApplication(sys.argv)
icon = Wind()
icon.show()
sys.exit(app.exec_())

How to close it with "Shift+Ctrl+Alt+Q" combination, for example?
I know that it is possible to connect hotkeys via QKeySequence, but I can't.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zaswed, 2017-10-15
@TomasHuk

with active window

def keyPressEvent(self, e):
        if int(e.modifiers()) == (QtCore.Qt.ControlModifier +
                                      QtCore.Qt.AltModifier +
                                      QtCore.Qt.ShiftModifier):
            if e.key() == QtCore.Qt.Key_Q:
                self.close_window()

D
DarkMode, 2017-10-15
@DarkMode

try using the keyboard module https://github.com/boppreh/keyboard

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question