我正在尝试使用PyQt5在python 3中构建GUI界面。我已将QPushButton连接到QLineEdit,以便可以清除用户编写的内容,例如(在类中工作):
self.textboxA = QLineEdit(self)
self.buttonA = QPushButton('Clear', self)
self.buttonA.clicked.connect(self.textboxA.clear)
但是,如果我有多个文本框,并且希望特定按钮仅清除选定数目的文本框(例如,仅文本框A和C),该怎么办?我尝试过:
self.textboxA = QLineEdit(self)
self.textboxB = QLineEdit(self)
self.textboxC = QLineEdit(self)
self.buttonA = QPushButton('Clear', self)
self.buttonA.clicked.connect(self.textboxA.clear,
self.textboxC.clear)
但它一直在给我写TypeError:预期为Qt.ConnectionType,而不是“ builtin_function_or_method”
我不太了解,
有人可以帮助我吗?
非常感谢
答案 0 :(得分:1)
尝试一下:
import sys
from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget,
QLineEdit, QPushButton, QVBoxLayout)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
main_layout = QVBoxLayout()
self.textboxA = QLineEdit()
self.textboxB = QLineEdit()
self.textboxC = QLineEdit()
self.buttonA = QPushButton('Clear -> textboxA, textboxC')
self.buttonA.clicked.connect(self.clearTextboxs)
main_layout.addWidget(self.textboxA)
main_layout.addWidget(self.textboxB)
main_layout.addWidget(self.textboxC)
main_layout.addWidget(self.buttonA)
central_widget = QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
def clearTextboxs(self):
self.textboxA.clear()
self.textboxC.clear()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
答案 1 :(得分:1)
最简单,最优雅的方法是连接每个功能:
self.buttonA.clicked.connect(self.textboxA.clear)
self.buttonA.clicked.connect(self.textboxC.clear)
如果有很多,只需使用for循环:
for textbox in (self.textboxA, self.textboxB, self.textboxC):
self.buttonA.clicked.connect(textbox.clear)