我有多个按钮&想知道点击了哪个按钮。我发现了错误,并且知道sender()
函数必须与QWidget
而不是类对象一起使用,但我无法找到解决方案。
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
"""Widget code here"""
self.btn1 = QtWidgets.QPushButton(self.widget)
"""Button properties here"""
self.btn1.setObjectName("btn1")
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QtWidgets.QPushButton(self.widget)
self.btn2.setObjectName("btn2")
self.btn2.clicked.connect(self.btnListener)
"""..... more buttons"""
def btnListener(self):
sender_button = self.sender() # Error Ui_MainWindow has no attribute sender
print(sender_button)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
您需要从MainWindow继承Ui_MainWindow才能使其正常工作。
class Ui_MainWindow(MainWindow):
...
答案 1 :(得分:0)
在Qt 4.8和python2.7中,我使用partial传递多个参数来发出信号。
from functools import partial
...
def initGui(self):
...
self.btn1.clicked.connect(partial(self.btnListener, "btn1"))
self.btn2.clicked.connect(partial(self.btnListener, "btn2"))
...
def btnListener(self, button_name):
print('button_name {}'.format(button_name))
...
使用这种方法,您可以知道单击了哪个按钮。
我希望能帮助您在QT5中找到相同的内容。
答案 2 :(得分:0)
以下代码运行良好。
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class DlgMain(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Yeh lay")
self.resize(400,400)
self.btn1 = QPushButton("btn1", self)
self.btn1.move(200,200)
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QPushButton("btn2", self)
self.btn2.move(200,230)
self.btn2.clicked.connect(self.btnListener)
def btnListener(self):
rbt = self.sender()
print(rbt.text())
if __name__ =='__main__':
app = QApplication(sys.argv)
dlgMain = DlgMain()
dlgMain.show()
sys.exit(app.exec_())
答案 3 :(得分:0)
这里有一个方法:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindow, self).__init__()
self.setupUI()
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
"""Widget code here"""
self.btn1 = QtWidgets.QPushButton(self.widget)
"""Button properties here"""
self.btn1.setObjectName("btn1")
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QtWidgets.QPushButton(self.widget)
self.btn2.setObjectName("btn2")
self.btn2.clicked.connect(self.btnListener)
"""..... more buttons"""
def btnListener(self):
sender_button = self..sender()
print(sender_button.text())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 4 :(得分:-1)
根据@MrLeeh建议&它已经有效了
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
"""Widget code here"""
self.btn1 = QtWidgets.QPushButton(self.widget)
"""Button properties here"""
self.btn1.setObjectName("btn1")
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QtWidgets.QPushButton(self.widget)
self.btn2.setObjectName("btn2")
self.btn2.clicked.connect(self.btnListener)
"""..... more buttons"""
def btnListener(self):
sender_button = self.MainWindow.sender()
print(sender_button.text())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())