API
showEvent(QShowEvent) # 控件显示时调用;
closeEvent(QCloseEvent) # 控件关闭时调用
moveEvent(QMoveEvent) # 控件移动时调用
# 窗口显示时已经移动了两次,移动的判定是x、y是否变化。
resizeEvent(QResizeEvent) # 控件调整大小时调用
# 窗口显示时已经改变了一次;
enterEvent(QEvent) # 鼠标进入时触发
leaveEvent(QEvent) # 鼠标离开时触发
# 可通过这个显示被选中的阴影效果。
mousePressEvent(QMouseEvent) # 鼠标按下时触发
QMouseEvent.button # 返回产生事件的按钮
# 需要注意的是,对于鼠标移动事件,该函数返回值总是Qt.NoButton
QMouseEvent.buttons # 返回产生事件的按钮状态
# 函数返回当前按下的所有按钮,按钮状态可以是
# Qt.LeftButton 左键
# Qt.RightButton 右键
# Qt.MidButton 中键
# 或运算组合 多键同时按下
QMouseEvent.flags
QMouseEvent.globalX() # 获取该对象的全局X轴位置,相对于桌面而言
QMouseEvent.globalY() # 获取该对象的全局Y轴位置,相对于桌面而言
QMouseEvent.globalPos() # 获取该对象的全局位置(x, y),相对于桌面而言
QMouseEvent.screenPos() # 获取该对象的全局位置(x.0, y.0),相对于桌面而言
QMouseEvent.x() # 获取该对象的局部X轴位置,相对于父对象而言
QMouseEvent.y() # 获取该对象的局部y轴位置,相对于父对象而言
QMouseEvent.pos() # 获取该对象的局部位置(x, y),相对于父对象而言
QMouseEvent.localPos() # 获取该对象的局部位置(x.0, y.0),相对于父对象而言
mouseReleaseEvent(QMouseEvent) # 一次按下加一次释放(控件的空间范围内)就是一次单击。
mouseDoubleClickEvent(QMouseEvent)
mouseMoveEvent(QMouseEvent)
# setMouseTracking(True)设置鼠标跟踪后,没有按下的移动也能触发。
keyPressEvent(QKeyEvent) # 键盘按下时调用
keyReleaseEvent(QKeyEvent) # 键盘释放时调用
# 如果想监测是哪个按键被按下:查看QKeyEvent事件对象。
# 键盘事件中,用户按下的是哪个键会存储在事件函数的 QKeyEvent 事件对象中
QKeyEvent.count() # 按下/释放按键的个数
QKeyEvent.isAutoRepeat() # 是否自动重复(长按不松就是自动重复)
QKeyEvent.key() # 获取用户按下的普通键(A、B...),返回是代号
QKeyEvent.matches() #
QKeyEvent.modifiers() # 获取用户按下的修饰键(Ctrl、Shift、Alt... 按下后系统没有任何操作的键)。返回一个对象
控件.grabKeyboard()
控件.grabGesture()
控件.grabMouse()
控件.grabShortcut()
focusInEvent(QFocusEvent) # 获取焦点时调用
focusOutEvent(QFocusEvent) # 失去焦点时调用
dragEnterEvent(QDragEnterEvent) # 拖拽进入控件时调用
dragLeaveEvent(QDragLeaveEvent) # 拖拽离开控件时调用
dragMoveEvent(QDragMoveEvent) # 拖拽在控件内移动时调用
dropEvent(QDropEvent) # 拖拽放下时调用
paintEvent(QPaintEvent) # 显示控件, 更新控件时调用;
changeEvent(QEvent) # 窗体改变, 字体改变时调用。
contextMenuEvent(QContextMenuEvent) # 访问右键菜单时调用。
inputMethodEvent(QInputMethodEvent) # 输入法调用
# 0.导入包和模块
from PyQt5.Qt import *
import sys
# 继承类、定义自己的方法
class Window(QWidget):
# 初始化
def __init__(self):
super(Window, self).__init__()
self.setWindowTitle("事件消息的学习")
self.resize(500, 500)
self.setup_ui()
# 存放所有子控件以及子控件的配置操作
def setup_ui(self):
label = QLabel(self)
label.setText("标签签")
def showEvent(self, a0: QShowEvent) -> None:
print("窗口被展示")
def closeEvent(self, a0: QCloseEvent) -> None:
print("窗口被关闭")
def moveEvent(self, a0: QMoveEvent) -> None:
print("窗口被移动")
def resizeEvent(self, a0: QResizeEvent) -> None:
print("窗口改变了尺寸大小")
def enterEvent(self, a0: QEvent) -> None:
print("鼠标进来了")
self.setStyleSheet("background-color: yellow;")
def leaveEvent(self, a0: QEvent) -> None:
print("鼠标离开了")
self.setStyleSheet("background-color: green;")
def mousePressEvent(self, a0: QMouseEvent) -> None:
print("鼠标被按下")
def mouseReleaseEvent(self, a0: QMouseEvent) -> None:
print("鼠标被释放")
def mouseDoubleClickEvent(self, a0: QMouseEvent) -> None:
print("鼠标双击")
def mouseMoveEvent(self, a0: QMouseEvent) -> None:
print("鼠标移动")
def keyPressEvent(self, a0: QKeyEvent) -> None:
print("键盘某一按键被按下")
def keyReleaseEvent(self, a0: QKeyEvent) -> None:
print("键盘某一按键被释放")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
事件转发
from PyQt5.Qt import *
import sys
# 自定义Widget类,继承自QWidget
class MyWidget(QWidget):
def mousePressEvent(self, a0: QMouseEvent) -> None:
print('顶层窗口被点击了')
class midWindow(QWidget):
def mousePressEvent(self, a0: QMouseEvent) -> None:
print('中间控件被点击了')
class MyLabel(QLabel):
def mousePressEvent(self, ev: QMouseEvent) -> None:
print('标签被点击了')
app = QApplication(sys.argv)
window = MyWidget()
window.resize(500, 500)
window.setWindowTitle('事件转发案例')
mid_win = midWindow(window)
mid_win.resize(300, 300)
mid_win.move(100, 100)
mid_win.setAttribute(Qt.WA_StyledBackground, True)
mid_win.setStyleSheet("background-color:yellow;")
label = MyLabel(mid_win)
label.setText('我是一个标签')
label.move(100,150)
label.setStyleSheet("background-color: red;")
window.show()
sys.exit(app.exec_())
class MyLabel(QLabel):
pass
# def mousePressEvent(self, ev: QMouseEvent) -> None:
# print('标签被点击了')
class midWindow(QWidget):
pass
# def mousePressEvent(self, a0: QMouseEvent) -> None:
# print('中间控件被点击了')
from PyQt5.Qt import *
import sys
# 自定义Widget类,继承自QWidget
class MyWidget(QWidget):
def mousePressEvent(self, a0: QMouseEvent) -> None:
print('顶层窗口被点击了')
class midWindow(QWidget):
def mousePressEvent(self, a0: QMouseEvent) -> None:
print('中间控件被点击了')
class MyLabel(QLabel):
pass
# def mousePressEvent(self, ev: QMouseEvent) -> None:
# print('标签被点击了')
class MyButton(QPushButton):
pass
app = QApplication(sys.argv)
window = MyWidget()
window.resize(500, 500)
window.setWindowTitle('事件转发案例')
mid_win = midWindow(window)
mid_win.resize(300, 300)
mid_win.move(100, 100)
mid_win.setAttribute(Qt.WA_StyledBackground, True)
mid_win.setStyleSheet("background-color:yellow;")
label = MyLabel(mid_win)
label.setText('我是一个标签')
label.move(100,150)
label.setStyleSheet("background-color: red;")
btn = MyButton(mid_win)
btn.setText('我是一个按钮')
btn.move(100, 50)
window.show()
sys.exit(app.exec_())
evt.ignore():标记事件被忽略,继续上传给父控件。
evt.accept():标记事件被处理,不会继续上传给父控件。
evt.isAccepted():看事件是否被处理,返回True或False
class MyLabel(QLabel):
def mousePressEvent(self, ev: QMouseEvent) -> None:
# pass
print('标签被点击了')
# 标记事件被忽略,继续往上转发给父控件
ev.ignore()
# 获取事件处理状态
print('事件处理状态:', ev.isAccepted())
class MyLabel(QLabel):
def mousePressEvent(self, ev: QMouseEvent) -> None:
# pass
print('标签被点击了')
# 标记事件被忽略,继续往上转发给父控件
# ev.ignore()
# 标记事件被处理,不再继续往上转发给父控件
ev.accept()
# 获取事件处理状态
print('事件处理状态:', ev.isAccepted())
事件消息案例
from PyQt5.Qt import *
import sys
class MyLabel(QLabel):
def enterEvent(self, a0: QEvent) -> None:
# 鼠标进入时触发
self.setText('欢迎光临')
def leaveEvent(self, a0: QEvent):
# 鼠标离开时触发
self.setText('谢谢惠顾')
app = QApplication(sys.argv)
window = QWidget()
window.resize(500, 500)
window.setWindowTitle('事件消息案例1')
label = MyLabel(window)
label.move(100, 100)
label.resize(300, 100)
label.setStyleSheet("background-color: cyan;font-size:40px;")
window.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class MyLabel(QLabel):
def keyPressEvent(self, ev: QKeyEvent) -> None:
# 判断键盘按下的普通键是 Tab
if ev.key() == Qt.Key_Tab:
self.setText("Tab")
# 判断键盘按下的修饰键是 Ctrl 并且 普通键是 S
elif ev.modifiers() == Qt.ControlModifier and ev.key() == Qt.Key_S:
self.setText("Ctrl + S")
# 判断键盘按下的修饰键是 Ctrl 和 Shift 并且 普通键是 A
elif ev.modifiers() == Qt.ControlModifier | Qt.ShiftModifier and ev.key() == Qt.Key_A:
self.setText("Ctrl + Shift + A")
app = QApplication(sys.argv)
window = QWidget()
window.resize(500, 500)
window.setWindowTitle("事件消息案例2")
# keyPressEvent(QKeyEvent) # 键盘按下时调用
# keyReleaseEvent(QKeyEvent) # 键盘释放时调用
label = MyLabel(window)
label.resize(400, 100)
label.move(50, 100)
label.setStyleSheet("background-color: cyan;font-size:40px;")
# 设置控件捕获键盘
label.grabKeyboard()
window.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.mous_y = 0
self.mous_x = 0
self.setWindowTitle('事件消息案例3')
self.resize(500, 500)
def mousePressEvent(self, ev: QMouseEvent) -> None:
# 获取鼠标点击时相对于控件的x、y坐标
self.mous_x = ev.x()
self.mous_y = ev.y()
def mouseMoveEvent(self, ev: QMouseEvent) -> None:
# 获取鼠标在x轴和y轴方向移动的距离
move_x = ev.x() - self.mous_x
move_y = ev.y() - self.mous_y
# 设置窗口的x坐标和y坐标。
self.move(self.x()+move_x, self.y()+move_y)
def mouseReleaseEvent(self, ev: QMouseEvent) -> None:
print('鼠标被释放了')
app = QApplication(sys.argv)
window = MyWidget()
window.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.mouse_y = 0
self.mouse_x = 0
self.setWindowTitle('事件消息案例3')
self.resize(500, 500)
def mousePressEvent(self, ev: QMouseEvent) -> None:
# 获取鼠标点击时相对于控件的x、y坐标
self.mouse_x = ev.x()
self.mouse_y = ev.y()
def mouseMoveEvent(self, ev: QMouseEvent) -> None:
# 获取鼠标在x轴和y轴方向移动的距离
move_x = ev.x() - self.mouse_x
move_y = ev.y() - self.mouse_y
# 设置窗口的x坐标和y坐标。
self.move(self.x()+move_x, self.y()+move_y)
def mouseReleaseEvent(self, ev: QMouseEvent) -> None:
print('鼠标被释放了')
app = QApplication(sys.argv)
window = MyWidget()
window.setMouseTracking(True)
window.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.state = False
self.mouse_y = 0
self.mouse_x = 0
self.setWindowTitle('事件消息案例3')
self.resize(500, 500)
def mousePressEvent(self, ev: QMouseEvent) -> None:
# 获取鼠标点击时相对于控件的x、y坐标
self.mouse_x = ev.x()
self.mouse_y = ev.y()
self.state = True
def mouseMoveEvent(self, ev: QMouseEvent) -> None:
if self.state:
# 获取鼠标在x轴和y轴方向移动的距离
move_x = ev.x() - self.mouse_x
move_y = ev.y() - self.mouse_y
# 设置窗口的x坐标和y坐标。
self.move(self.x()+move_x, self.y()+move_y)
def mouseReleaseEvent(self, ev: QMouseEvent) -> None:
print('鼠标被释放了')
app = QApplication(sys.argv)
window = MyWidget()
window.setMouseTracking(True)
window.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.state = False
self.mouse_y = 0
self.mouse_x = 0
self.setWindowTitle('事件消息案例3')
self.resize(500, 500)
def mousePressEvent(self, ev: QMouseEvent) -> None:
# 获取鼠标点击时相对于控件的x、y坐标
self.mouse_x = ev.x()
self.mouse_y = ev.y()
self.state = True
def mouseMoveEvent(self, ev: QMouseEvent) -> None:
if self.state:
# 获取鼠标在x轴和y轴方向移动的距离
move_x = ev.x() - self.mouse_x
move_y = ev.y() - self.mouse_y
# 设置窗口的x坐标和y坐标。
self.move(self.x()+move_x, self.y()+move_y)
def mouseReleaseEvent(self, ev: QMouseEvent) -> None:
self.state = False
app = QApplication(sys.argv)
window = MyWidget()
window.setMouseTracking(True)
window.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.state = False
self.mouse_y = 0
self.mouse_x = 0
self.setWindowTitle('事件消息案例3')
self.resize(500, 500)
def mousePressEvent(self, ev: QMouseEvent) -> None:
if ev.button() == Qt.LeftButton:
# 获取鼠标点击时相对于控件的x、y坐标
self.mouse_x = ev.x()
self.mouse_y = ev.y()
self.state = True
def mouseMoveEvent(self, ev: QMouseEvent) -> None:
if self.state:
# 获取鼠标在x轴和y轴方向移动的距离
move_x = ev.x() - self.mouse_x
move_y = ev.y() - self.mouse_y
# 设置窗口的x坐标和y坐标。
self.move(self.x()+move_x, self.y()+move_y)
def mouseReleaseEvent(self, ev: QMouseEvent) -> None:
self.state = False
app = QApplication(sys.argv)
window = MyWidget()
window.setMouseTracking(True)
window.show()
sys.exit(app.exec_())