PyQt5 事件
2019-09-22 python 1844
import sys from PyQt5.QtWidgets import QWidget, QApplication, QMessageBox, QDesktopWidget, QPushButton, QLabel from PyQt5.QtCore import Qt, QEvent from PyQt5.QtGui import QPainter, QPixmap class Mouse(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): print("init ui") self.btnTest = QPushButton("安安", self) self.move_lbl = QLabel("鼠标位置", self) self.mousepos_lbl = QLabel("鼠标坐标", self) self.mousepos_lbl.setStyleSheet("QLabel{color:#fff;font-size:16px;}") self.mousepos_lbl.setGeometry(100, 100, 100, 100) self.mousewinpos_lbl = QLabel("win鼠标坐标", self) self.mousewinpos_lbl.setStyleSheet("QLabel{color:#fff;font-size:16px;}") self.setGeometry(300, 300, 500, 500) self.setWindowTitle("鼠标坐标获取") self.center() def paintEvent(self, event): painter = QPainter(self) pic = QPixmap("bg.jpg") painter.drawPixmap(0, 0, self.width(), self.height(), pic) # super().paintEvent(event) def resizeEvent(self, event): w = self.width() h = self.height() wbtn = self.btnTest.width() hbtn = self.btnTest.height() self.btnTest.setGeometry((w - wbtn) / 2, (h - hbtn) / 2, wbtn, hbtn) def closeEvent(self, event): defaultBtn = QMessageBox.NoButton result = QMessageBox.question(self, "Question 消息框", "closeEvent 触发", QMessageBox.Yes | QMessageBox.No, defaultBtn) if result == QMessageBox.Yes: event.accept() # 接受 else: event.ignore() # 不关闭 def mousePressEvent(self, event): pt = event.pos() # pt= event.localPos() # 鼠标位置: QPoint 与 pos() 相同 # pt = event.windowPos() # 鼠标位置: QPointF 与 pos() 相同 if (event.button() == Qt.LeftButton): self.move_lbl.setText("(x,y)=(%d,%d)" % (pt.x(), pt.y())) rect = self.move_lbl.geometry() self.move_lbl.setGeometry(pt.x(), pt.y(), rect.width(), rect.height()) self.move_lbl.adjustSize() def mouseMoveEvent(self, event): pt = event.pos() win = event.globalPos() # pt= event.localPos() # 鼠标位置: QPoint 与 pos() 相同 # pt = event.windowPos() # 鼠标位置: QPointF 与 pos() 相同 self.mousepos_lbl.setText("(x,y)=(%d,%d)" % (pt.x(), pt.y())) self.mousepos_lbl.adjustSize() self.mousewinpos_lbl.setText("(x,y)=(%d,%d)" % (win.x(), win.y())) self.mousewinpos_lbl.adjustSize() def keyPressEvent(self, event): rect = self.btnTest.geometry() key = event.key() print(key) if key in set([Qt.Key_A, Qt.Key_Left]): self.btnTest.setGeometry(rect.left() - 20, rect.top(), rect.width(), rect.height()) elif key in set([Qt.Key_D, Qt.Key_Right]): self.btnTest.setGeometry(rect.left() + 20, rect.top(), rect.width(), rect.height()) elif key in set([Qt.Key_W, Qt.Key_Up]): self.btnTest.setGeometry(rect.left(), rect.top() - 20, rect.width(), rect.height()) elif key in set([Qt.Key_S, Qt.Key_Down]): self.btnTest.setGeometry(rect.left(), rect.top() + 20, rect.width(), rect.height()) self.btnTest.setText("(x,y)=(%d,%d)" % (rect.left(), rect.top())) def center(self): qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) if __name__ == "__main__": app = QApplication(sys.argv) mouse = Mouse() mouse.show() sys.exit(app.exec_())
python监听事件
from pynput.keyboard import Listener # pip install pynput def on_press(key): print(key) with Listener(on_press=on_press) as listerner: listerner.join() Listener.stop()
很赞哦! (0)
相关文章
文章评论
-
-
-
0条评论