Skip to content

Commit 3057d7e

Browse files
committed
add 29
1 parent f708ee7 commit 3057d7e

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@
4848

4949
* p27_显示matplotlib图表 [源码下载](p27_显示matplotlib图表) [博文地址](https://xugaoxiang.com/2022/05/03/pyqt5-27-matplotlib/)
5050

51-
* p28_鼠标事件 [源码下载](p28_鼠标事件) [博文地址](https://xugaoxiang.com/2022/05/15/pyqt5-28-mouse-event/)
51+
* p28_鼠标事件 [源码下载](p28_鼠标事件) [博文地址](https://xugaoxiang.com/2022/05/15/pyqt5-28-mouse-event/)
52+
53+
* p29_键盘事件 [源码下载](p29_键盘事件) [博文地址](https://xugaoxiang.com/2022/05/31/pyqt5-29-keyboard-event/)

p29_键盘事件/main.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import sys
2+
3+
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox, QShortcut
4+
from PyQt5.QtCore import Qt
5+
from PyQt5.QtGui import QKeySequence
6+
7+
from ui import Ui_MainWindow
8+
9+
10+
class MainWindow(QMainWindow, Ui_MainWindow):
11+
12+
def __init__(self, parent=None):
13+
super(MainWindow, self).__init__(parent)
14+
self.setupUi(self)
15+
16+
self.pushButton.clicked.connect(self.onClick)
17+
# 给按钮操作设置一个快捷键
18+
self.pushButton.setShortcut(QKeySequence(Qt.Key_A))
19+
20+
# 或者这样
21+
# self.shortcut_button = QShortcut(QKeySequence('Ctrl+S'), self)
22+
# self.shortcut_button.activated.connect(self.onClick)
23+
24+
def onClick(self):
25+
print('onClick.')
26+
27+
def keyPressEvent(self, event):
28+
# 判断按下的是什么键
29+
if event.key() == Qt.Key_D:
30+
print('keyPressEvent, Key_D')
31+
32+
# ctrl 修饰键
33+
if event.modifiers() & Qt.ControlModifier:
34+
print('Ctrl+D')
35+
# alt 修饰键
36+
elif event.modifiers() & Qt.AltModifier:
37+
print('Alt+D')
38+
# shift 修饰键
39+
elif event.modifiers() & Qt.ShiftModifier:
40+
print('Shift+D')
41+
42+
def keyReleaseEvent(self, event):
43+
print('keyReleaseEvent')
44+
45+
46+
if __name__ == '__main__':
47+
app = QApplication(sys.argv)
48+
windows = MainWindow()
49+
windows.show()
50+
sys.exit(app.exec_())

p29_键盘事件/project.ui

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>MainWindow</class>
4+
<widget class="QMainWindow" name="MainWindow">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>800</width>
10+
<height>600</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>MainWindow</string>
15+
</property>
16+
<widget class="QWidget" name="centralwidget">
17+
<widget class="QPushButton" name="pushButton">
18+
<property name="geometry">
19+
<rect>
20+
<x>280</x>
21+
<y>180</y>
22+
<width>201</width>
23+
<height>81</height>
24+
</rect>
25+
</property>
26+
<property name="font">
27+
<font>
28+
<pointsize>20</pointsize>
29+
</font>
30+
</property>
31+
<property name="text">
32+
<string>按 钮</string>
33+
</property>
34+
</widget>
35+
</widget>
36+
<widget class="QMenuBar" name="menubar">
37+
<property name="geometry">
38+
<rect>
39+
<x>0</x>
40+
<y>0</y>
41+
<width>800</width>
42+
<height>26</height>
43+
</rect>
44+
</property>
45+
</widget>
46+
<widget class="QStatusBar" name="statusbar"/>
47+
</widget>
48+
<resources/>
49+
<connections/>
50+
</ui>

p29_键盘事件/ui.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Form implementation generated from reading ui file '.\project.ui'
4+
#
5+
# Created by: PyQt5 UI code generator 5.9.2
6+
#
7+
# WARNING! All changes made in this file will be lost!
8+
9+
from PyQt5 import QtCore, QtGui, QtWidgets
10+
11+
class Ui_MainWindow(object):
12+
def setupUi(self, MainWindow):
13+
MainWindow.setObjectName("MainWindow")
14+
MainWindow.resize(800, 600)
15+
self.centralwidget = QtWidgets.QWidget(MainWindow)
16+
self.centralwidget.setObjectName("centralwidget")
17+
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
18+
self.pushButton.setGeometry(QtCore.QRect(280, 180, 201, 81))
19+
font = QtGui.QFont()
20+
font.setPointSize(20)
21+
self.pushButton.setFont(font)
22+
self.pushButton.setObjectName("pushButton")
23+
MainWindow.setCentralWidget(self.centralwidget)
24+
self.menubar = QtWidgets.QMenuBar(MainWindow)
25+
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
26+
self.menubar.setObjectName("menubar")
27+
MainWindow.setMenuBar(self.menubar)
28+
self.statusbar = QtWidgets.QStatusBar(MainWindow)
29+
self.statusbar.setObjectName("statusbar")
30+
MainWindow.setStatusBar(self.statusbar)
31+
32+
self.retranslateUi(MainWindow)
33+
QtCore.QMetaObject.connectSlotsByName(MainWindow)
34+
35+
def retranslateUi(self, MainWindow):
36+
_translate = QtCore.QCoreApplication.translate
37+
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
38+
self.pushButton.setText(_translate("MainWindow", "按 钮"))
39+

0 commit comments

Comments
 (0)