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.
After we press Space button.
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:
After we press Ctrl + n:
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:
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
- [PyQt5] Tutorial(1) Install PyQt5 and print "Hello World!"
- [PyQt5] Tutorial(2) QLabel, QLineEdit, QPushButton
- [PyQt5] Tutorial(3) QMainWindow, QIcon, QPixmap, QPalette
- [PyQt5] Tutorial(4) Menu, Toolbar
- [PyQt5] Tutorial(5) ProgressBar, HorizontalSlider, QDial
- [PyQt5] Tutorial(6) comboBox、BoxLayout
- [PyQt5] Tutorial(7) hide, show, auto fit window size
- [PyQt5] Tutorial(8) QTimer, QlcdNumber
- [PyQt5] Tutorial(9) Use QCalendar to create a calendar component
- [PyQt5] Tutorial(10) Use keyboard to enter command and listen mouse clicked
- [PyQt5] Tutorial(11) Use QColorDialog to let user select color