用于QLineEdit PyQT5的Matchbox-Keyboard输入

时间:2018-03-15 18:35:38

标签: python pyqt pyqt5 virtual-keyboard

我想要做的是当用户(通过触摸屏)点击可编辑的QEditLine时,我希望它显示用户输入的Matchbox-Keyboard。未单击时不显示键盘。

我已经阅读了C文档和一些C示例,但是我也迷失了,因为它也跳转到了Python。我看到人们提到设置“焦点”可以有人解释这个吗?

r = 9005678662260991

2 个答案:

答案 0 :(得分:1)

建议不要通过分配函数self.nameInput.mousePressEvent = self.showKeyboard来覆盖事件方法,因为mousePressEvent QLineEdit的任务丢失并可能导致意外事件。

另外,mousePressEvent不是合适的事件,因为您可以按QLineEdit多次,然后它会被调回键盘。

更好的选择是在focusInEvent中启动它并在focusOutEvent中将其删除:

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

class MatchBoxLineEdit(QLineEdit):
    def focusInEvent(self, e):
        try:
            subprocess.Popen(["matchbox-keyboard"])
        except FileNotFoundError:
            pass

    def focusOutEvent(self,e):
        subprocess.Popen(["killall","matchbox-keyboard"])

class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('GUI TESTS')

        widget = QWidget()
        self.setCentralWidget(widget)
        lay = QVBoxLayout(widget)

        self.userNameLabel = QLabel("What is your name?")
        self.nameInput = MatchBoxLineEdit()

        lay.addWidget(self.userNameLabel)
        lay.addWidget(self.nameInput)

        self.setGeometry(
            QStyle.alignedRect(
                Qt.LeftToRight,
                Qt.AlignCenter,self.sizeHint(), 
                qApp.desktop().availableGeometry()
                )
            )


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = App()
    w.show()
    sys.exit(app.exec_())

答案 1 :(得分:0)

import sys
import os
from PyQt5.QtWidgets import QApplication, QFileDialog, QSlider, QComboBox, QCheckBox, QWidget, QMainWindow, QPushButton, QLabel, QGridLayout, QGroupBox, QRadioButton, QMessageBox, QLineEdit
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import pyqtSlot, Qt


class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'GUI TESTS'
        self.left = 0
        self.top = 0
        self.width = 800
        self.height = 400
        self.statusBarMessage = "GUI TEST"
        self.currentSprite = 'TEST.png'
        self.btn1Active = False
        self.btn2Active = False
        self.btn3Active = False
        self.btn4Active = False
        self.btn5Active = False
        self.btn6Active = False
        self.btn7Active = False
        self.btn8Active = False
        self.saveLocationDir = ""
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.statusBar().showMessage(self.statusBarMessage)

        self.userNameLabel = QLabel(self)
        self.userNameLabel.move(0,125)
        self.userNameLabel.setText("What is your name?")
        self.userNameLabel.resize(120,20)

        self.nameInput = QLineEdit(self)
        self.nameInput.move(0,145)
        self.nameInput.resize(200,32)
        self.nameInput.setEchoMode(0)
        self.nameInput.mousePressEvent=self.showKeyboard

    @pyqtSlot()
    def showKeyboard(self,event):
        if event.button() == QtCore.Qt.LeftButton:
            QtWidgets.QLineEdit.mousePressEvent(self, event)
            command = "matchbox-keyboard"
            os.system(command)

您可以覆盖mousePressEvent并实现该功能