Skip to content

[PyQt5] Tutorial(10) Use keyboard to enter command and listen mouse clicked

If we want to use keyboard and mouse in our interface in PyQt5, we have to set it up. So I think recoding about these is a good idea.


shortcut setting

First we take a look for sample code:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'board.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(303, 123)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(0, 0, 301, 91))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 303, 25))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "The number you selected: "))


The interface code is used Qt Designer and PyUIC.

And then we test a common shortcut:

# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from board import Ui_MainWindow


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ctrl_n = QShortcut(QKeySequence("Space"), self)
        self.ctrl_n.activated.connect(self.displayEvent)

    def displayEvent(self):
        self.ui.label.setText('The number you selected: Space')


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())


The important code is:

QShortcut(QKeySequence("Space"), self)

This code will return an object, if this object activated by Space button, it will show.

self.ctrl_n.activated.connect(self.displayEvent)

displayEvent() function will set the label content.

Before


After we press Space button.

After

QKeySequence("Space")

Not only a single button, but also a combination of multiple buttons. (For example: ctrl + n)

# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from board import Ui_MainWindow


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.ctrl_n = QShortcut(QKeySequence("Ctrl+n"), self)
        self.ctrl_n.activated.connect(self.displayEvent)

    def displayEvent(self):
        self.ui.label.setText('The number you selected: Ctrl+n')


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())


Output:

這張圖片的 alt 屬性值為空,它的檔案名稱為 image-56.png
Before


After we press Ctrl + n:

After

Mouse event

The mouse event is actually very simple, it is to rewrite the mosePressEvent() event that we want to determine.

# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from board import Ui_MainWindow


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.ui.label.setText('The number you selected: Left!')

        if event.button() == Qt.RightButton:
            self.ui.label.setText('The number you selected: Right!')


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())


Output:

Before


If we press left mouse button:


If we press right mouse button:

If you have any function want to make, just write it under the if-block.


Read More

Leave a Reply