I'm trying to figure out how to use a virtual keyboard for a touchscreen, using python 3.8 and PyQt5 on Windows.
I saw that Qt had his own plugin QtVirtualKeyboard. I pretty much followed what has been done in this link, first installing Qt 5.15 with the Virtual Keyboard support, and then setting up the environment variables.
A simple code example would be this :
import os
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.line_edit = None
self.init_ui()
def init_ui(self):
self.line_edit = QLineEdit()
self.line_edit2 = QLineEdit()
self.layout = QVBoxLayout()
self.main_widget = QWidget()
self.main_widget.setLayout(self.layout)
self.layout.addWidget(self.line_edit)
self.layout.addWidget(self.line_edit2)
self.setCentralWidget(self.main_widget)
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
The idea would be to display the keyboard when touching the line edit. For now, the window is corretly displayed but no keyboard will pop out. I tried to set up ("QT_DIR", "QT_PLUGIN_PATH",...) as in the link above, but nothing worked.
I know I'm missing something there but can't figure out what. Thank you for your help !
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…