尝试使用QTextEdit小部件的内置'find'函数,但是当我尝试从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()调用中的字符串文字(例如“What”)替换对QLineEdit文本属性的引用(第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()
答案 0 :(得分:1)
更改此
self.fileEdit.find(self.findLine.text)
到
self.fileEdit.find(self.findLine.text())
因为它的函数不是属性:)