QLineEdit PyQT5 输入上的火柴盒键盘 [英] Matchbox-Keyboard on input for QLineEdit PyQT5
问题描述
我想要做的是当用户(通过触摸屏)点击可编辑的 QEditLine 时,我希望它显示火柴盒键盘以供用户输入.未单击时不显示键盘.
What I'm trying to do is when a user (via a touchscreen) clicks on an editable QEditLine I want it to show the Matchbox-Keyboard for user input. When it is not clicked do not show the keyboard.
我已经阅读了 C 文档和一些 C 示例,但我也迷失了,因为我也跳到了 Python.我看到有人提到设置焦点",有人可以向我解释一下吗?
I've gone through the C documentation, and a few C examples, but I'm lost as too make the jump to Python. I see people mentioning setting the "focus" can someone explain this too me?
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)
@pyqtSlot()
def showKeyboard(self):
command = "matchbox-keyboard"
os.system(command)
推荐答案
不建议通过分配函数 self.nameInput.mousePressEvent = self.showKeyboard
来覆盖 events 方法,因为任务QLineEdit
的 mousePressEvent
丢失,可能导致意外事件.
It is not recommended to override the events method by assigning a function self.nameInput.mousePressEvent = self.showKeyboard
since the tasks of the mousePressEvent
of the QLineEdit
are lost and could cause unexpected events.
此外,mousePressEvent
不是合适的事件,因为您可以多次按下 QLineEdit
并且它会被回调到键盘.
Also, mousePressEvent
is not the appropriate event since you can press the QLineEdit
many times and it would be called back to the keyboard.
更好的选择是在 focusInEvent
中启动它并在 focusOutEvent
中删除它:
A better option is to launch it in focusInEvent
and delete it in 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_())
这篇关于QLineEdit PyQT5 输入上的火柴盒键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!