1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/python3

'''
Программа для демонстрации перехвата событий мыши и клавиатуры
виджетом.
'''

from PyQt4 import QtGui, QtCore
import sys


class MyWidget(QtGui.QWidget):
    def __init__(self):
        super().__init__()

    def mousePressEvent(self, e):
        print("Mouse pressed at", e.x(), e.y())

    def mouseReleaseEvent(self, e):
        print("Mouse released at", e.x(), e.y())

    def mouseMoveEvent(self, e):
        print("Mouse moved to", e.x(), e.y())

    def keyPressEvent(self, e):
        print("Key pressed, text is", repr(e.text()))

    def keyReleaseEvent(self, e):
        print("Key released, text is", repr(e.text()))


def main():
    app = QtGui.QApplication(sys.argv)
    w = MyWidget()
    w.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()