gpt4 book ai didi

python - 在 QPlainTextEdit() 中禁用回车(按 Enter 键)

转载 作者:行者123 更新时间:2023-12-01 00:41:01 25 4
gpt4 key购买 nike

这就是我的示例:

Disable Return

文本区域是一个 QPlainTextEdit() 对象,因为我希望文本换行到第二行。我认为这是最好的小部件选择。

用户最多只能在此框中输入 90 个字符,因此我不需要很大的文本区域。

我想禁用 Enter 键(回车)。我让它工作了,但看起来很老套,而且我认为它不能跨平台工作(例如:Mac)。

当然,必须有更好的方法来防止 QPlainTextEdit 对象中的回车键事件?

我当前的解决方案说明

在下面,您可以看到我正在检查是否发生 IndexError,因为当 QPlainTextEdit 框中没有任何内容时 last_value 会抛出 IndexError 。然后,我获取最后一个字符并询问它是否等于新行。如果是,我将重新设置文本而不添加新行,并将光标移至末尾。

    def some_event(self):
try:
last_value = self.field.toPlainText()[-1]
if last_value == '\n':
print('You Pressed Enter!', repr(last_value))
self.field.setPlainText(self.field.toPlainText()[:-1])
self.field.moveCursor(QTextCursor.End)
except IndexError:
print('Index Error occurred')
pass

完整代码最小工作示例:

from PyQt5.QtWidgets import (QWidget, QMainWindow, QGridLayout, QPushButton,
QApplication, QPlainTextEdit, QLabel)
from PyQt5.QtGui import QTextCursor

class BasicWindow(QMainWindow):
def __init__(self):
super().__init__()

self.initWindow()

def initWindow(self):
self.setGeometry(400, 300, 400, 100)

self.grid = QGridLayout()

self.label = QLabel('Description Line 1')
self.grid.addWidget(self.label, 0, 0)

self.field = QPlainTextEdit()
self.field.setMaximumHeight(40)

self.field.textChanged.connect(self.some_event)

#TODO how to disable enter/return key events in this field?
self.grid.addWidget(self.field, 1, 0)

self.button = QPushButton('Some Button')
self.grid.addWidget(self.button)

self.centralWidget = QWidget()
self.centralWidget.setLayout(self.grid)
self.setCentralWidget(self.centralWidget)

def some_event(self):
try:
last_value = self.field.toPlainText()[-1]
if last_value == '\n':
print('You Pressed Enter!', repr(last_value))
self.field.setPlainText(self.field.toPlainText()[:-1])
self.field.moveCursor(QTextCursor.End)
except IndexError:
print('Index Error occurred')
pass

if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = BasicWindow()
window.show()
sys.exit(app.exec_())

最佳答案

一种选择是覆盖 keyPressEvent QPlainTextEdit的方法:

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QTextCursor
from PyQt5.QtWidgets import (QWidget, QMainWindow, QGridLayout, QPushButton,
QApplication, QPlainTextEdit, QLabel)


class PlainTextEdit(QPlainTextEdit):
def keyPressEvent(self, event):
if event.key() in (Qt.Key_Return, Qt.Key_Enter):
return
super().keyPressEvent(event)


class BasicWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initWindow()

def initWindow(self):
self.setGeometry(400, 300, 400, 100)
self.label = QLabel("Description Line 1")
self.field = PlainTextEdit()
self.field.setMaximumHeight(40)
self.button = QPushButton("Some Button")

self.centralWidget = QWidget()
grid = QGridLayout(self.centralWidget)
grid.addWidget(self.label, 0, 0)
grid.addWidget(self.field, 1, 0)
grid.addWidget(self.button)
self.setCentralWidget(self.centralWidget)


if __name__ == "__main__":
import sys

app = QApplication(sys.argv)
window = BasicWindow()
window.show()
sys.exit(app.exec_())

实现相同逻辑的另一个选项是使用 eventFilter() .

from PyQt5.QtCore import QEvent, Qt
from PyQt5.QtGui import QTextCursor
from PyQt5.QtWidgets import (QWidget, QMainWindow, QGridLayout, QPushButton,
QApplication, QPlainTextEdit, QLabel)

class BasicWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initWindow()

def initWindow(self):
self.setGeometry(400, 300, 400, 100)
self.label = QLabel("Description Line 1")
self.field = QPlainTextEdit()
self.field.setMaximumHeight(40)
self.button = QPushButton("Some Button")

self.field.installEventFilter(self)

self.centralWidget = QWidget()
grid = QGridLayout(self.centralWidget)
grid.addWidget(self.label, 0, 0)
grid.addWidget(self.field, 1, 0)
grid.addWidget(self.button)
self.setCentralWidget(self.centralWidget)

def eventFilter(self, obj, event):
if obj is self.field and event.type() == QEvent.KeyPress:
if event.key() in (Qt.Key_Return, Qt.Key_Enter):
return True
return super().eventFilter(obj, event)


if __name__ == "__main__":
import sys

app = QApplication(sys.argv)
window = BasicWindow()
window.show()
sys.exit(app.exec_())

关于python - 在 QPlainTextEdit() 中禁用回车(按 Enter 键),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57351709/

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