gpt4 book ai didi

使用 QTextEdit.find() 时出现 Python pyqt 错误 : unexpected type 'builtin_function_or_method'

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:51 27 4
gpt4 key购买 nike

尝试使用 QTextEdit 小部件的内置“查找”功能,但当我尝试从 QLineEdit 小部件传递文本时,出现以下错误:

Traceback (most recent call last):
File "C:\SVN\Gocator\Trunk\Gocator\GoPy\Scripts\testfind.pyw", line 52, in on_find_button_clicked
self.fileEdit.find(self.findLine.text)
TypeError: QTextEdit.find(str, QTextDocument.FindFlags options=0): argument 1 has unexpected type 'builtin_function_or_method'

我已经查看了 QTextEdit 类的文档,内容不多,但我不明白为什么它会给我这个错误。有趣的是,如果我在 find() 调用中将 QLineEdit 文本属性的引用替换为字符串文字(例如“What”)(第 52 行:self.fileEdit.find(self.findLine.text)),它会起作用。

我的测试代码非常简单,所以我认为这只是我在我眼前看不到的东西。有人看到我哪里出错了或者甚至遇到了同样的问题吗?这是我的测试脚本(我只安装了 Qt4):

#!/usr/bin/env python

# Needs Qt5 (recommended) or Qt4

# PyQt5: run pip3 install pyqt5
# PyQt4: from http://www.riverbankcomputing.com/software/pyqt/download

import shelve
import sys

sys.path.append('../GoPy')
sys.path.append('../../GoPy')

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class ControlEngine:
def __init__(self):

self.shelf = shelve.open("ui_control.shelf")

class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)

self.engine = ControlEngine()
self.create_ui()

def create_ui(self):
mainLayout = QVBoxLayout()

# View group

viewGroup = QGroupBox("File View")
viewLayout = QVBoxLayout()
self.fileEdit = QTextEdit("Today at Safeway, here's what we have for you.")
self.fileEdit.setFont(QFont("Courier New", 10))
viewLayout.addWidget(self.fileEdit)

self.findButton = QPushButton("Find Next Word")
self.findButton.clicked.connect(self.on_find_button_clicked)
self.findLine = QLineEdit("What")
viewLayout.addWidget(self.findLine)
viewLayout.addWidget(self.findButton)

viewGroup.setLayout(viewLayout)
mainLayout.addWidget(viewGroup)

self.setLayout(mainLayout)
self.fileEdit.moveCursor(1)

@pyqtSlot(int, int)

def on_find_button_clicked(self):
self.fileEdit.find(self.findLine.text)

class App(QApplication):
def __init__(self, argv):
QApplication.__init__(self, argv)

def start(self):
mainWindow = MainWindow()
self.mainWindow = mainWindow
mainWindow.resize(300, 300)
mainWindow.setWindowTitle("Test Find")
mainWindow.show()

return self.exec_()

def main():
app = App(sys.argv)
sys.exit(app.start())


if __name__ == '__main__':
main()

最佳答案

改变这个

    self.fileEdit.find(self.findLine.text)

    self.fileEdit.find(self.findLine.text())

因为它是函数而不是属性:)

关于使用 QTextEdit.find() 时出现 Python pyqt 错误 : unexpected type 'builtin_function_or_method' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43564713/

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