zoukankan      html  css  js  c++  java
  • 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  

  • 相关阅读:
    关于配置文件权衡,.config VS .xml
    Google不支持小于12px字体 终极办法
    两个表循环的复杂度分析 征集
    SQL 计算列
    5分钟上手写ECharts的第一个图表
    NGif, Animated GIF Encoder for .NET
    Chart系列(一):Chart的基本元素
    一张广告图片引起的思维DFS
    洛谷 P2580 于是他错误的点名开始了
    洛谷 P1496 火烧赤壁
  • 原文地址:https://www.cnblogs.com/wudeng/p/9473890.html
Copyright © 2011-2022 走看看