PyQt:如何连接自动添加的按钮
PyQt: How to connect automatically added buttons
我正在尝试编写的程序为列表中的每个元素添加了一个按钮
for elem in add_new:
counter = add_new.index(elem)
name = add_new_names[counter]
self.button = QPushButton(name)
self.button.setObjectName('button_' + str(name))
self.button.setFixedWidth(200)
self.button.setFixedHeight(200)
self.stacked_layout.addWidget(self.button)
self.mygroupbox.setLayout(self.stacked_layout)
self.scroll.setWidget(self.mygroupbox)
这可行,但现在我想将这些按钮连接到 event/function。如果我添加
self.button.clicked.connect(self.clickevent)
无论按下什么按钮,它都会(显然)执行该功能。有没有办法将自动添加的按钮连接到不同的 functions/events?
编辑:
提供更多代码
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.first_time = "True"
self.setAcceptDrops(True)
self.mainLayout = QVBoxLayout()
self.drop_label = ImageLabel()
self.mainLayout.addWidget(self.drop_label)
self.myform = QHBoxLayout()
self.mygroupbox = QGroupBox('selected pdf files')
self.scroll = QScrollArea()
self.scroll.setWidgetResizable(True)
self.scroll.setFixedHeight(300)
self.mainLayout.addWidget(self.scroll)
self.stacked_layout = QHBoxLayout()
self.setLayout(self.mainLayout)
def dragEnterEvent(self, event):
if self.find_pdf(event.mimeData()):
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if self.find_pdf(event.mimeData()):
event.accept()
else:
event.ignore()
def dropEvent(self, event):
add_new.clear()
add_new_names.clear()
urls = self.find_pdf(event.mimeData())
if urls:
for url in urls:
self.path = url.toLocalFile()
self.file_name = QFileInfo(self.path)
self.filename = self.file_name.baseName()
link_list.append(self.path)
title_list.append(self.filename)
list_of_links.append(self.path)
add_new.append(self.path)
add_new_names.append(self.filename)
event.accept()
self.button_creating()
else:
event.ignore()
def button_creating(self):
for elem in add_new:
counter = add_new.index(elem)
name = add_new_names[counter]
self.button = QPushButton(name)
self.button.setObjectName('button_' + str(name))
self.button.setFixedWidth(200)
self.button.setFixedHeight(200)
self.stacked_layout.addWidget(self.button)
self.mygroupbox.setLayout(self.stacked_layout)
self.scroll.setWidget(self.mygroupbox)
self.button.clicked.connect(self.clickevent)
def find_pdf(self, mimedata):
urls = list()
db = QMimeDatabase()
for url in mimedata.urls():
mimetype = db.mimeTypeForUrl(url)
if mimetype.name() == "application/pdf":
urls.append(url)
return urls
def clickevent(self):
print(self.button.text())
app = QApplication(sys.argv)
demo = AppDemo()
demo.showMaximized()
sys.exit(app.exec_())
如果我没理解错的话,您正在寻找函数 self.sender()
.. 使用 self.sender()
可以找到按下的按钮。如果你有很多按钮,特别有用,所有按钮都调用相同的函数,并且你想知道哪个按钮被按下了。
然后你可以使用 pressed_btn = self.sender()
并访问 QPushButton
的所有功能。这里我有一个例子来解释:
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
class Example(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.setLayout(layout)
for i in range(10):
button = QPushButton(str(i), self)
button.setObjectName('button_' + str(i))
button.clicked.connect(self.test)
layout.addWidget(button)
def test(self):
button = self.sender()
print(f'Button Object name: {button.objectName()}\nButton Text: {button.text()}')
if __name__ == '__main__':
app = QApplication([])
example = Example()
example.show()
app.exec()
单击按钮 0 时的输出:
我正在尝试编写的程序为列表中的每个元素添加了一个按钮
for elem in add_new:
counter = add_new.index(elem)
name = add_new_names[counter]
self.button = QPushButton(name)
self.button.setObjectName('button_' + str(name))
self.button.setFixedWidth(200)
self.button.setFixedHeight(200)
self.stacked_layout.addWidget(self.button)
self.mygroupbox.setLayout(self.stacked_layout)
self.scroll.setWidget(self.mygroupbox)
这可行,但现在我想将这些按钮连接到 event/function。如果我添加
self.button.clicked.connect(self.clickevent)
无论按下什么按钮,它都会(显然)执行该功能。有没有办法将自动添加的按钮连接到不同的 functions/events?
编辑: 提供更多代码
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.first_time = "True"
self.setAcceptDrops(True)
self.mainLayout = QVBoxLayout()
self.drop_label = ImageLabel()
self.mainLayout.addWidget(self.drop_label)
self.myform = QHBoxLayout()
self.mygroupbox = QGroupBox('selected pdf files')
self.scroll = QScrollArea()
self.scroll.setWidgetResizable(True)
self.scroll.setFixedHeight(300)
self.mainLayout.addWidget(self.scroll)
self.stacked_layout = QHBoxLayout()
self.setLayout(self.mainLayout)
def dragEnterEvent(self, event):
if self.find_pdf(event.mimeData()):
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if self.find_pdf(event.mimeData()):
event.accept()
else:
event.ignore()
def dropEvent(self, event):
add_new.clear()
add_new_names.clear()
urls = self.find_pdf(event.mimeData())
if urls:
for url in urls:
self.path = url.toLocalFile()
self.file_name = QFileInfo(self.path)
self.filename = self.file_name.baseName()
link_list.append(self.path)
title_list.append(self.filename)
list_of_links.append(self.path)
add_new.append(self.path)
add_new_names.append(self.filename)
event.accept()
self.button_creating()
else:
event.ignore()
def button_creating(self):
for elem in add_new:
counter = add_new.index(elem)
name = add_new_names[counter]
self.button = QPushButton(name)
self.button.setObjectName('button_' + str(name))
self.button.setFixedWidth(200)
self.button.setFixedHeight(200)
self.stacked_layout.addWidget(self.button)
self.mygroupbox.setLayout(self.stacked_layout)
self.scroll.setWidget(self.mygroupbox)
self.button.clicked.connect(self.clickevent)
def find_pdf(self, mimedata):
urls = list()
db = QMimeDatabase()
for url in mimedata.urls():
mimetype = db.mimeTypeForUrl(url)
if mimetype.name() == "application/pdf":
urls.append(url)
return urls
def clickevent(self):
print(self.button.text())
app = QApplication(sys.argv)
demo = AppDemo()
demo.showMaximized()
sys.exit(app.exec_())
如果我没理解错的话,您正在寻找函数 self.sender()
.. 使用 self.sender()
可以找到按下的按钮。如果你有很多按钮,特别有用,所有按钮都调用相同的函数,并且你想知道哪个按钮被按下了。
然后你可以使用 pressed_btn = self.sender()
并访问 QPushButton
的所有功能。这里我有一个例子来解释:
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
class Example(QWidget):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.setLayout(layout)
for i in range(10):
button = QPushButton(str(i), self)
button.setObjectName('button_' + str(i))
button.clicked.connect(self.test)
layout.addWidget(button)
def test(self):
button = self.sender()
print(f'Button Object name: {button.objectName()}\nButton Text: {button.text()}')
if __name__ == '__main__':
app = QApplication([])
example = Example()
example.show()
app.exec()
单击按钮 0 时的输出: