PyQt5从开环中创建的多个按钮中检索ID和值[重复]
Posted
技术标签:
【中文标题】PyQt5从开环中创建的多个按钮中检索ID和值[重复]【英文标题】:PyQt5 retrieving ID and values from multiple Buttons created in an open loop [duplicate] 【发布时间】:2021-02-08 19:10:55 【问题描述】:我正在尝试基于条目 (QLineEdit) 创建按钮 (QPushButtons)。我的想法是,我希望用户能够创建任意数量的按钮,只需在输入框中添加新文本,然后按“添加标签”(见下图)。
虽然我能够做到这一点,但我现在无法检索每个按钮的标签值,因为我使用的过程会删除所有以前的值(我只能检索最后输入的值)。我希望能够在单击每个按钮时打印每个特定的标签值。
我的代码如下:
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLineEdit
import sys
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(100, 100, 1500, 1500)
self.setWindowTitle("My Program")
self.labelButtons = [] # List of all the buttons displaying labels
self.eraseButtons = [] # List of all the buttons displaying "X"
self.Yposition = 50
self.initUI()
def initUI(self):
self.labelEntry = QLineEdit(self)
self.labelEntry.move(50, self.Yposition)
self.labelEntry.resize(300, 40)
self.addLabelButton = QPushButton(self)
self.addLabelButton.setText("Add Label")
self.addLabelButton.move(400, self.Yposition)
self.addLabelButton.resize(300, 40)
self.addLabelButton.clicked.connect(self.addNewLabel)
def addNewLabel(self):
self.Yposition += 50
self.newLabelName = self.labelEntry.text()
self.labelButtons.append(self.createButtonLabel(self.newLabelName))
self.eraseButtons.append(self.eraseButtonLabel())
self.updatelabels()
def createButtonLabel(self, labelname):
self.button = QPushButton(self)
self.button.setText(str(labelname))
self.button.resize(300, 40)
self.button.move(50, self.Yposition)
self.button.clicked.connect(self.printbutton)
return self.button
def eraseButtonLabel(self):
self.buttonErase = QPushButton(self)
self.buttonErase.setText("X")
self.buttonErase.resize(40, 40)
self.buttonErase.move(360, self.Yposition)
self.buttonErase.clicked.connect(self.printbutton)
return self.buttonErase
def updatelabels(self):
for button in self.labelButtons:
button.show()
for button in self.eraseButtons:
button.show()
def printbutton(self):
print(self.button.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
<!-- end snippet -->
【问题讨论】:
我不确定,但是当您单击按钮时,PyQt
应该发送 event
以及 widget
创建此 event
的信息(其中 button
是单击),然后您可以用它来访问widget
。我不确定,但也许你需要def printbutton(self, event):
?
您可以添加包含所有导入的代码 - 运行它并测试一些想法会更简单。
def printbutton(self): print(self.sender().text())
。或者使用QButtonGroup。
感谢 self.sender(),它确实允许从每个按钮中检索文本。关于删除按钮你有什么想法吗?同样对于@furas,对不起,我忘了粘贴导入,它们是: from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLineEdit import sys
下次将导入(以及其他代码、数据和错误消息)放在有问题的地方,不要在评论中 - 它将更具可读性,更多人会看到它。
【参考方案1】:
使用谷歌pyqt clicked event
我发现你必须使用
def printbutton(self):
widget = self.sender()
print(widget.text())
ZetCode:Events and signals in PyQt5
编辑:
至于erease
按钮 - 您应该从createButtonLabel
获取按钮并将其发送到eraseButtonLabel
labelbutton = self.createButtonLabel(self.newLabelName)
erasebutton = self.eraseButtonLabel(labelbutton)
您可以使用lambda
来为函数赋值
def eraseButtonLabel(self, labelbutton):
# ... code ...
self.buttonErase.clicked.connect(lambda: self.erasebutton(labelbutton))
函数应该得到这个参数
def erasebutton(self, button):
widget = self.sender()
print('clicked:', widget.text())
print(' erase:', button.text())
或者您可以将button
分配给buttonErase
中的自己的变量
def eraseButtonLabel(self, labelbutton):
# ... code ...
self.buttonErase.assigned_button = labelbutton
self.buttonErase.clicked.connect(self.erasebutton)
并在函数中使用它
def erasebutton(self):
widget = self.sender()
print('clicked:', widget.text())
print(' erase:', widget.assigned_button.text())
同时使用两种方法的完整代码,但您只需要一种方法。
from PyQt5.QtWidgets import *
import sys
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(100, 100, 1500, 1500)
self.setWindowTitle("My Program")
self.labelButtons = [] # List of all the buttons displaying labels
self.eraseButtons = [] # List of all the buttons displaying "X"
self.Yposition = 50
self.initUI()
def initUI(self):
self.labelEntry = QLineEdit(self)
self.labelEntry.move(50, self.Yposition)
self.labelEntry.resize(300, 40)
self.addLabelButton = QPushButton(self)
self.addLabelButton.setText("Add Label")
self.addLabelButton.move(400, self.Yposition)
self.addLabelButton.resize(300, 40)
self.addLabelButton.clicked.connect(self.addNewLabel)
def addNewLabel(self):
self.Yposition += 50
self.newLabelName = self.labelEntry.text()
labelbutton = self.createButtonLabel(self.newLabelName)
erasebutton = self.eraseButtonLabel(labelbutton)
self.labelButtons.append(labelbutton)
self.eraseButtons.append(erasebutton)
self.updatelabels()
def createButtonLabel(self, labelname):
self.button = QPushButton(self)
self.button.setText(str(labelname))
self.button.resize(300, 40)
self.button.move(50, self.Yposition)
self.button.clicked.connect(self.printbutton)
return self.button
def eraseButtonLabel(self, labelbutton):
self.buttonErase = QPushButton(self)
self.buttonErase.setText("X")
self.buttonErase.resize(40, 40)
self.buttonErase.move(360, self.Yposition)
self.buttonErase.assigned_button = labelbutton
self.buttonErase.clicked.connect(lambda: self.erasebutton(labelbutton))
#self.buttonErase.clicked.connect(self.erasebutton)
return self.buttonErase
def updatelabels(self):
for button in self.labelButtons:
button.show()
for button in self.eraseButtons:
button.show()
def printbutton(self):
print('clicked:', self.sender().text())
def erasebutton(self, button):
widget = self.sender()
print('clicked:', widget.text())
print(' erase:', button.text())
print(' erase:', widget.assigned_button.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
编辑:
其他方法是创建自己的小部件,它同时具有按钮labelbutton
和erasebutton
,然后erasebutton
只能直接访问自己的labelbutton
。
顺便说一句:出于类似的原因,我会将按钮保持成对
self.buttons.append([labelbutton, erasebutton])
而不是分隔列表
self.labelButtons.append(labelbutton)
self.eraseButtons.append(erasebutton)
我创建自己的小部件的示例。
from PyQt5.QtWidgets import *
import sys
class MyWidget(QWidget):
def __init__(self, parent, labelname, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.resize(350, 40)
self.labelButton = QPushButton(self)
self.labelButton.setText(str(labelname))
self.labelButton.resize(300, 40)
self.labelButton.move(0, 0)
self.labelButton.clicked.connect(self.printbutton)
self.buttonErase = QPushButton(self)
self.buttonErase.setText("X")
self.buttonErase.resize(40, 40)
self.buttonErase.move(310, 0)
self.buttonErase.clicked.connect(self.erasebutton)
self.show()
def printbutton(self):
print('clicked:', self.labelButton.text())
def erasebutton(self):
print('clicked:', self.buttonErase.text())
print(' erase:', self.labelButton.text())
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(100, 100, 1500, 1500)
self.setWindowTitle("My Program")
self.widgets = []
self.Yposition = 50
self.initUI()
def initUI(self):
self.labelEntry = QLineEdit(self)
self.labelEntry.move(50, self.Yposition)
self.labelEntry.resize(300, 40)
self.addLabelButton = QPushButton(self)
self.addLabelButton.setText("Add Label")
self.addLabelButton.move(400, self.Yposition)
self.addLabelButton.resize(300, 40)
self.addLabelButton.clicked.connect(self.addNewLabel)
def addNewLabel(self):
self.Yposition += 50
text = self.labelEntry.text()
widget = MyWidget(self, text)
widget.move(50, self.Yposition)
self.widgets.append(widget)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
【讨论】:
谢谢,它确实可以很好地检索每个按钮的文本值。您还有关于“删除”按钮的提示吗? 您应该从createButtonLabel
获取button
并将其发送到eraseButtonLabel
- 它应该使用额外的参数连接点击 - 这个button
查看答案中的新代码。
非常感谢,我正在看。将按钮保持成对非常好。我现在尝试在单击 X 按钮时删除该对。
我添加了一个例子,我用两个按钮创建了自己的小部件。以上是关于PyQt5从开环中创建的多个按钮中检索ID和值[重复]的主要内容,如果未能解决你的问题,请参考以下文章