pyqt按键检测

Posted wudeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyqt按键检测相关的知识,希望对你有一定的参考价值。

# _*_ coding: utf-8 _*_
__author__ = ‘pythonwu‘
__date__ = "2018/8/9 10:47"
# textboxValue = self.textbox.text()
import sys
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5 import QtCore,QtGui
from PyQt5.QtCore import *
from PyQt5.Qt import QLineEdit,QTextEdit


class EmittingStream(QtCore.QObject):
textWritten = QtCore.pyqtSignal(str) #定义一个发送str的信号
def write(self, text):
self.textWritten.emit(str(text))

class Window(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setGeometry(300, 300, 300, 200)
self.setFixedWidth(300)
self.setFixedHeight(200)
self.setWindowTitle(‘按键检测‘)
# self.textbox = QLineEdit(self)
# self.textbox.move(20,20)
# self.textbox.resize(260,100)
self.centralwidget = QWidget(self)
self.centralwidget.setObjectName("centralwidget")
self.textEdit = QTextEdit(self)
self.textEdit.setGeometry(QtCore.QRect(0, 0, 731, 301))
self.textEdit.setObjectName("textEdit")
self.textEdit.setReadOnly(True)

sys.stdout = EmittingStream(textWritten=self.outputWritten)
sys.stderr = EmittingStream(textWritten=self.outputWritten)
self.show()

def outputWritten(self, text):
cursor = self.textEdit.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
cursor.insertText(text)
self.textEdit.setTextCursor(cursor)
self.textEdit.ensureCursorVisible()

# 检测键盘回车按键
def keyPressEvent(self, event):
print("按下:" + str(event.key()))
# 举例
if(event.key() == Qt.Key_Escape):
print(‘测试:ESC‘)
if(event.key() == Qt.Key_A):
print(‘测试:A‘)
if(event.key() == Qt.Key_1):
print(‘测试:1‘)
if(event.key() == Qt.Key_Enter):
print(‘测试:Enter‘)
if(event.key() == Qt.Key_Space):
print(‘测试:Space‘)
if(event.key() == Qt.Key_Control):
print(‘测试:Ctrl‘)
if(event.key() == Qt.Key_Delete):
print(‘测试: Delete‘)
if(event.key() == Qt.Key_Alt):
print(‘测试: Alt‘)

def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
print("鼠标左键点击")
elif event.button() == Qt.RightButton:
print("鼠标右键点击")
elif event.button() == Qt.MidButton:
print("鼠标中键点击")
if __name__ == ‘__main__‘:
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())

技术分享图片

打包 pyinstaller  

 















































































以上是关于pyqt按键检测的主要内容,如果未能解决你的问题,请参考以下文章

Pyqt搭建YOLOV5目标检测界面

51单片机学习笔记3 -- 按键输入检测

51单片机学习笔记3 -- 按键输入检测

PyQt5 像素级碰撞检测

PyQT4 WheelEvent?如何检测***是不是被使用过?

PyQt QTextEdit:检测更改事件