通过Enter键调用功能

时间:2017-12-19 11:22:33

标签: python python-3.x pyqt pyqt5 qkeyevent

当输入键盘键按下QPushButton("点击")时,如何使(on_click)工作?它只与mouse_click

交互
iris %>%   group_by(Species) %>% 
     mutate(Count = n()) %>%
     group_by(Species, Count) %>%
     summarize_at(vars(starts_with("Sepal")), funs(mean, sd)) -> dt_stat2
dt_stat2

1 个答案:

答案 0 :(得分:0)

您必须覆盖keyPressEvent方法:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Example(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        self.label = QLabel("",self)
        self.label.move(100, 100)

        self.button = QPushButton('click', self)
        self.button.move(100, 50)
        self.button.clicked.connect(self.on_click)

        self.setGeometry(500, 150, 200, 200)
        self.show()    

    def on_click(self):
        self.label.setText("Hello")

    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Return:
            self.on_click()


if __name__ == '__main__':
   app = QApplication(sys.argv)
   ex = Example()
   sys.exit(app.exec_())