python - PyQt,文本光标
问题描述
我一直在用 QTextEdit 对象构建一个 GUI,我想添加一个 QPushButton 将光标向下移动到新行。
编辑:有人建议这个问题可能会回答我的问题:Moving the cursor in a PyQt5 text edit doesn't work
但这不是因为在我的程序崩溃后调试器说“QTextCursor 对象没有属性'Left'。” 在该答案下的评论中,它说尝试“setTextCursor”,但又一次......同样的问题。
编辑:更新示例
class Window(QWidget):
def __init__(self):
super().__init__()
self.layout = QVBoxLayout()
self.w = QTextEdit()
self.cursor = self.w.textCursor()
self.button = QPushButton('Button')
self.button.clicked.connect(self.nextBlock)
# Add to layout
self.layout.addWidget(self.button)
self.layout.addWidget(self.w)
self.setLayout(self.layout)
def nextBlock(self):
self.w.moveCursor(QTextCursor.NextBlock)
QTimer.singleShot(0, self.w.setFocus)
def main():
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()
解决方案
推荐阅读
- python-3.x - 使用命令时如何确定提供正确参数的用户?
- c - Go 错误的 errno
- javascript - 当我尝试使用 permissionOverwrites 添加权限时,代码会完全更改它
- r - 更改标题的背景颜色并将剥离的灰度设置为行 kable
- r - R timeseries ggseasonplot - 参数暗示不同的行数:508、127
- python - Django URLs 文件将不同的 URL 重定向到错误的 html 文件
- r - 根据平均值删除列
- java - Java Swing JEditorPane 在选择周围插入粗体标签
- django - Django for...empty 时使用不同的容器
- node.js - 在 Dockerfile 中安装更高版本的 nodejs