python - Python PyQt QPushButton 替换/覆盖方法
问题描述
我有这样的代码:
self.addBtn.clicked.connect(self.add)
if self.added:
self.addBtn.clicked.connect(self.remove)
但是当我点击登录按钮时,它会执行self.add
,然后self.remove
。有没有办法删除/覆盖self.add
?谢谢
解决方案
我不确定您需要做什么,但请尝试以下示例:
import sys
from PyQt5.QtWidgets import (QPushButton, QVBoxLayout, QApplication,
QWidget, QLabel)
class Test(QWidget):
def __init__(self):
super().__init__()
self.flag = True
self.label = QLabel()
self.addBtn = QPushButton("add")
self.addBtn.clicked.connect(self.add_remove)
vbox = QVBoxLayout(self)
vbox.addWidget(self.label)
vbox.addWidget(self.addBtn)
def add_remove(self):
if self.flag:
print("You clicked the button: `add`")
self.flag = False
self.addBtn.setText("remove")
# Do something ...
self.label.setText("Hello Tom Rowbotham")
else:
print("You clicked the button: `remove`")
self.flag = True
self.addBtn.setText("add")
# Do something ...
self.label.setText("")
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Test()
w.show()
sys.exit(app.exec_())
推荐阅读
- c# - navPage.SetHideNavigationBarSeparator(true) 不再适用于 Xamarin 4.5 最新的任何解决方案,不适用于以下代码
- java - 无法在 IF 语句中使用布尔值
- git - 如何在 Git 本地完全排除子目录
- java - charAt() 方法如何从字符串中获取数字并将它们放入 Java 中的新字符串中?
- c# - 通过 _ViewImports (.net core) 在视图中使用 httpContextAccessor
- python - Pandas 与唯一索引/列组合字典的相关矩阵
- javascript - 离屏 convertToBlob 很慢
- c - 当另一个更改时如何进行变量更改
- html - R Markdown 两个并排的表格 HTML - LaTex
- python - 使用 Python 查找序列