gpt4 book ai didi

python - 如何使用屏幕上的键盘将文本插入到 QLineEdit

转载 作者:行者123 更新时间:2023-12-01 07:40:44 31 4
gpt4 key购买 nike

我正在 python 上使用 PyQt5 制作一个登录 GUI 程序,在这个程序中我在此布局上创建了一个键盘。我的问题是,我无法使用我所做的键盘在 QLineEdit 上插入文本。我应该做什么来修复它?

这是我在 pyqt 上的第一个程序,我不知道如何修复它。

# x <-- 3

class MainWindow(QMainWindow): 
def __init__(self, x):
super().__init__()
self.L_ID = QLabel("User ID :",self)
self.L_ID.setFont(QFont('Arial', 16))
self.L_ID.move(25, 25)

self.entry_ID = QLineEdit(self)
self.entry_ID.move(130, 20)
self.entry_ID.resize(450,40)

self.L_Pass = QLabel("Pass :",self)
self.L_Pass.setFont(QFont('Arial', 16))
self.L_Pass.move(25, 85)

self.entry_Pass = QLineEdit(self)
self.entry_Pass.move(130, 80)
self.entry_Pass.resize(450,40)

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

names = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm','Del',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','Shift',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.', "'",'Enter']

positions = [(i + 1, j) for i in range(3) for j in range(13)]

x = 100
d = 100
i=0
j=135
for name in names:
self.pushButton = QPushButton(name, self)
if(name == 'Enter'):
self.pushButton.setGeometry(QRect(int("{}".format(d)), j, 85, 35))
else:
self.pushButton.setGeometry(QRect(int("{}".format(d)), j, 40, 35))
self.pushButton.setText(name)
text = self.pushButton.text()
self.pushButton.clicked.connect(lambda ch, text=text : print("{}".format(text)))
#self.pushButton.setObjectName("pushButton_{}".format(str(i).zfill(2)))
i += 1
d += 43
if(i == 14):
j +=45
x = 100
d = 100
i=0

#self.numButton = 4

pybutton = QPushButton('Space', self)
pybutton.clicked.connect(self.clickMethod)
pybutton.setGeometry(QRect(350, j+45, 100, 35))

#self.lay.addWidget(pybutton)
#self.lay.addStretch(1)

def clickMethod(self):
newBtn = QPushButton('New Button{}'.format(self.numButton), self)
self.numButton += 1
newBtn.clicked.connect(lambda : print("\nclicked===>> {}".format(newBtn.text())))
self.lay.addWidget(newBtn)


if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = MainWindow(3)
mainWin.setGeometry(50, 50, 800, 480)
mainWin.setFixedSize(800, 480) # 3 --> x
mainWin.show()
sys.exit( app.exec_() )

我可以向 shell 显示按钮的文本,但不能在 QLineEdit 上显示。如何使用屏幕键盘将文本插入到 QLineEdit。

最佳答案

对于通用解决方案,最好使用 QKeyEventQCoreApplication::postEvent(...) 发送到具有焦点的小部件(您必须删除按钮上的焦点)。

为了使逻辑简单,我们创建一个字典,将每个文本与 Qt::Key 相关联。

对于特殊情况,例如 DelShiftEnterSpace,必须修改文本。

from PyQt5 import QtCore, QtGui, QtWidgets

LETTERS = "abcdefghijklmnopqrstuvwxyz"
NUMBERS = "1234567890"
LUT = {
"a": QtCore.Qt.Key_A,
"b": QtCore.Qt.Key_B,
"c": QtCore.Qt.Key_C,
"d": QtCore.Qt.Key_D,
"e": QtCore.Qt.Key_E,
"f": QtCore.Qt.Key_F,
"g": QtCore.Qt.Key_G,
"h": QtCore.Qt.Key_H,
"i": QtCore.Qt.Key_I,
"j": QtCore.Qt.Key_J,
"k": QtCore.Qt.Key_K,
"l": QtCore.Qt.Key_L,
"m": QtCore.Qt.Key_M,
"n": QtCore.Qt.Key_N,
"o": QtCore.Qt.Key_O,
"p": QtCore.Qt.Key_P,
"q": QtCore.Qt.Key_Q,
"r": QtCore.Qt.Key_R,
"s": QtCore.Qt.Key_S,
"t": QtCore.Qt.Key_T,
"u": QtCore.Qt.Key_U,
"v": QtCore.Qt.Key_V,
"w": QtCore.Qt.Key_W,
"x": QtCore.Qt.Key_X,
"y": QtCore.Qt.Key_Y,
"z": QtCore.Qt.Key_Z,
"Del": QtCore.Qt.Key_Delete,
"Shift": QtCore.Qt.Key_Shift,
"Enter": QtCore.Qt.Key_Enter,
"Space": QtCore.Qt.Key_Space,
"1": QtCore.Qt.Key_1,
"2": QtCore.Qt.Key_2,
"3": QtCore.Qt.Key_3,
"4": QtCore.Qt.Key_4,
"5": QtCore.Qt.Key_5,
"6": QtCore.Qt.Key_6,
"7": QtCore.Qt.Key_7,
"8": QtCore.Qt.Key_8,
"9": QtCore.Qt.Key_9,
"0": QtCore.Qt.Key_0,
".": QtCore.Qt.Key_Period,
"'": QtCore.Qt.Key_Apostrophe,
}


class KeyBoard(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)

letters = LETTERS[: len(LETTERS) // 2], LETTERS[len(LETTERS) // 2 :]
numbers = NUMBERS + ".'"

grid_layout = QtWidgets.QGridLayout(self)

for i, (a, b) in enumerate(zip(*letters)):
for j, letter in enumerate((a, b)):
button = QtWidgets.QToolButton(
text=letter,
clicked=self.onClicked,
focusPolicy=QtCore.Qt.NoFocus,
)
button.setFixedSize(40, 35)
grid_layout.addWidget(button, j, i)

for i, number in enumerate(numbers):
button = QtWidgets.QToolButton(
text=number,
clicked=self.onClicked,
focusPolicy=QtCore.Qt.NoFocus,
)
button.setFixedSize(40, 35)
grid_layout.addWidget(button, 2, i)

for i, text in enumerate(("Del", "Shift")):
button = QtWidgets.QToolButton(
text=text, clicked=self.onClicked, focusPolicy=QtCore.Qt.NoFocus
)
button.setFixedSize(40, 35)
grid_layout.addWidget(button, i, 13)

button = QtWidgets.QToolButton(
text="Enter", clicked=self.onClicked, focusPolicy=QtCore.Qt.NoFocus
)
button.setFixedSize(85, 35)
grid_layout.addWidget(button, 2, 12, 1, 2)

button = QtWidgets.QToolButton(
text="Space", clicked=self.onClicked, focusPolicy=QtCore.Qt.NoFocus
)
button.setFixedSize(100, 35)
grid_layout.addWidget(
button, 3, 0, 1, 13, alignment=QtCore.Qt.AlignCenter
)
self.setFixedSize(self.sizeHint())

@QtCore.pyqtSlot()
def onClicked(self):
button = self.sender()
if button is None:
return
widget = QtWidgets.QApplication.focusWidget()

text = button.text()
key = LUT[text]
if text in ("Del", "Shift", "Enter", "Space"):
if text in ("Shift", "Enter"):
text = ""
elif text == "Space":
text = " "
elif text == "Del":
text = chr(0x7F)
event = QtGui.QKeyEvent(
QtCore.QEvent.KeyPress, key, QtCore.Qt.NoModifier, text
)
QtCore.QCoreApplication.postEvent(widget, event)


class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)

central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)

font = QtGui.QFont("Arial", 16)

user_id_label = QtWidgets.QLabel("User ID:", font=font)
user_id_lineedit = QtWidgets.QLineEdit(font=font)

pass_id_label = QtWidgets.QLabel("Pass:", font=font)
pass_id_lineedit = QtWidgets.QLineEdit(font=font)

keyboard = KeyBoard()

flay = QtWidgets.QFormLayout(central_widget)
flay.addRow(user_id_label, user_id_lineedit)
flay.addRow(pass_id_label, pass_id_lineedit)
lay = QtWidgets.QVBoxLayout()
lay.addWidget(keyboard, alignment=QtCore.Qt.AlignCenter)
flay.addRow(lay)


if __name__ == "__main__":
import sys

app = QtWidgets.QApplication(sys.argv)

w = MainWindow()
w.show()
sys.exit(app.exec_())

关于python - 如何使用屏幕上的键盘将文本插入到 QLineEdit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56730008/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com