roy*_*rek 2 python pyqt pyqt4 qt-signals qt-slot
我正在使用基于 PyQt4 的 PyQt。我使用的是 PyCharm 2017.3。我的python版本是3.4。
我正在尝试连接当我们单击鼠标从 QLineEdit 捕获内容时收到的信号。
class HelloWorld(QMainWindow, tD_ui.Ui_MainWindow):
# defining constructor
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.getContent()
self.putValues()
self.setWindowTitle("Downloader")
self.pushButton.mousePressEvent.connect(self.getContent)
Run Code Online (Sandbox Code Playgroud)
所以当我运行代码时,出现以下错误
Traceback (most recent call last):
File "C:/Project/Downloader/Implement.py", line 113, in <module>
helloworld = HelloWorld()
File "C:/Project/Downloader/Implement.py", line 18, in __init__
self.pushButton.mousePressEvent.connect(self.getContent)
AttributeError: 'builtin_function_or_method' object has no attribute 'connect'
Run Code Online (Sandbox Code Playgroud)
PS->请尽量避免解决方案中的遗留代码
mousePressEvent不是一个信号,所以你不应该使用 connect,你应该使用信号clicked:
self.pushButton.clicked.connect(self.getContent)
Run Code Online (Sandbox Code Playgroud)
加:
在Qt中,因此对于PyQt来说,有信号和事件,信号被发出并且事件必须被覆盖,在按钮的情况下,单击的任务是自然的并且是其逻辑固有的,因此已经创建了这个信号,但是在情况下QLabel 的没有该信号,但我们可以使用 mousePressEvent 事件来生成该信号,如下所示:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Label(QLabel):
clicked = pyqtSignal()
def mousePressEvent(self, event):
self.clicked.emit()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
w = Label("click me")
w.clicked.connect(lambda: print("clicked"))
w.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5908 次 |
| 最近记录: |