首先:它是使用PyQt5用Python3.6编写的。
我想每次单击按钮时创建标签。现在,我通过单击按钮时引用一个函数来执行此操作。我想保持这种方式。我的问题是,每次我按下“添加”按钮时,相同的def都以har标签和按钮开头。我想每次调用此函数时都创建不同的标签。
我想做些类似的事情:
....
self.pushButton_2.clicked.connect(self.new_line)
def new_line(self, Form):
self.label+str(i) = QtWidgets.QLabel()
self.label_2.setObjectName("labeli")
self.horizontalLayout.addWidget(self.labeli)
i = i + 1
.....
但是在这种情况下,我不是运行变量,因为它属于单词labeli
class Ui_Form(object):
def Table(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui2Form()
self.ui.setup2Ui(self.window)
self.window.show()
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(628, 196)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.label = QtWidgets.QLabel(Form)
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)
self.LCDAmount = QtWidgets.QLCDNumber(Form)
self.LCDAmount.setObjectName("LCDAmount")
#self.new_line(Form)
self.Knopfe(Form)
#neue Zeile einfügen
def new_line(self, Form):
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_2 = QtWidgets.QLabel()
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
spacerItem = QtWidgets.QSpacerItem(188, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.pushButton = QtWidgets.QPushButton()
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.verticalLayout.addLayout(self.horizontalLayout)
self.label_2.setText('filename')
self.pushButton.setText("Browse")
self.dlt = QtWidgets.QPushButton()
self.dlt.setObjectName("dlt")
self.dlt.setMaximumSize(QtCore.QSize(70, 40))
self.horizontalLayout.addWidget(self.dlt)
self.dlt.setText("Delete")
self.dlt.clicked.connect(self.label_2.close)
self.dlt.clicked.connect(self.pushButton.close)
self.dlt.clicked.connect(self.dlt.close)
self.dlt.clicked.connect(self.LCDupdateM)
self.pushButton.clicked.connect(self.openFileNamesDialog)
# Dialogfeld öffnen
def openFileNamesDialog(self):
fname = QFileDialog.getOpenFileName(None, "Datei öffnen", "C://", "XML only (*.xml)")
if fname:
f = str(fname)
f_path, f_ext, f_desc = f.split('.')
fsp = f_path.split('/')
fileName = fsp[len(fsp)-1]
filePath = f_path
filePathArray = filePath.split("'")
self.selectedFile = filePathArray[1] + '.xml'
print(f)
print(self.selectedFile)
print(fileName)
self.label_2.setText(fileName)
def Knopfe(self, Form):
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout_2.addWidget(self.pushButton_2)
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout_2.addWidget(self.pushButton_3)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.pushButton_2.clicked.connect(self.LCDupdate)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
self.pushButton_2.clicked.connect(self.new_line)
self.pushButton_3.clicked.connect(self.Table)
def LCDupdate(self,Form):
n = self.LCDAmount.intValue()
n=n+1
self.LCDAmount.display(str(n))
def LCDupdateM(self,Form):
n = self.LCDAmount.intValue()
n=n-1
self.LCDAmount.display(str(n))
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", " load your modules"))
self.pushButton_2.setText(_translate("Form", "Add module"))
self.pushButton_3.setText(_translate("Form", "Next"))
if __name__ == "__main__":
#bttn_clicks = 0
app = QtWidgets.QApplication(sys.argv)
ex = Ui_Form()
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
现在的问题是,我有一个光学工作的Gui,但是在 def openFileNamesDialog im引用了这些创建的行和标签,现在只有最后一行将标签从“文件名”更新为实际选择的文件,因为我无法再调用其他文件,因为它们都具有相同的名称。 / p>
我希望我能够解释我的问题和愿望,如果有人能帮助我,我将非常感谢。 我感谢每一个有用的评论。
最好的问候
答案 0 :(得分:0)
在不重构程序的情况下,将按钮信号连接到正确标签的最简单方法可能是将标签添加为openFileNamesDialog
的参数,然后在new_line
中将self.pushButton.clicked
连接到functools.partial
对象。这样,您只需要在脚本顶部添加from functools import partial
,将openFileNamesDialog
定义为
def openFileNamesDialog(self, label):
....
label.setText(f)
,然后在new_line()
中将最后一行替换为
self.pushButton.clicked.connect(partial(self.openFileNamesDialog, self.label_2))