pyside/pyqt:在单击按钮时从动态创建的 qlineedits 中获取值 [英] pyside / pyqt: Getting values from dynamically created qlineedits on button clicked

查看:99
本文介绍了pyside/pyqt:在单击按钮时从动态创建的 qlineedits 中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以根据用户输入创建许多 qlineedits 和按钮:

I have a program that creates a number of qlineedits and buttons depending on the users input:

在点击灰色的下一步"按钮后,上图中的 4 行添加了一个按钮.现在,我想在单击相应按钮时将用户的输入获取到函数中(单击创建镜头 1!--> 转到将exShot1"作为参数传递的函数).

On the image above 4 lines have been added with a button after the grayed out "Next" button was clicked. Now I want to get the input from the user into a function when the corresponding button is clicked (Click "Create Shot 1! --> goto a function with "exShot1" passed as an argument).

问题是我不知道如何在循环中创建每个 qline 和按钮的名称.我想我可以在循环中创建独特的变量,但这感觉不对.我尝试使用 setObjectName 但我不知道如何使用它来调用文本.我还对 Lamdba 进行了一次不成功的尝试(我觉得这可能是正确的方法)我相信这是必须在用户输入更改时获取名称和跟踪的组合.

The thing is I have no idea how to get the names of each qline and button when they are created in a loop. I guess I could create unique variables in the loop but that doesn't feel right. I have tried using setObjectName but I can't figure out how I can use that to call the text. I also made an unsuccessful attempt with Lamdba (which I have a feeling might be the right way to go somehow) I believe it's a combination of having to fetch the name and tracking when the user input is changed.

我已经尝试过 textChanged 并且我让它在循环的最后一个条目上工作,但不适用于其他 qlines 和按钮)

I have experimented with textChanged and I got it to work on the last entry of the loop but not for the other qlines and buttons)

相关代码:

while i <= int(seqNum):
    #create each widget
    self.createShotBtn = QtGui.QPushButton("Create Shot %s!" %str(self.shotNumberLst[i-1]))
    self.labelName = QtGui.QLabel(self)
    self.labelName.setText("Enter Name Of Shot %s!" %str(self.shotNumberLst[i-1]))
    self.shotName = QtGui.QLineEdit(self)
    self.shotName.setObjectName("shot"+str(i))

    #add widget to layout
    self.grid.addWidget(self.labelName, 11+shotjump,0)
    self.grid.addWidget(self.shotName,11+shotjump,1)
    self.grid.addWidget(self.createShotBtn, 11+shotjump,2)

    #Press button that makes magic happen
    self.createShotBtn.clicked.connect(???)

    i += 1

如果用户在所有行上输入输入并且只按下一个按钮将所有这些输入作为列表或字典传递(每个镜头"会添加更多行),那也很好

edit: It would also be fine if the user entered input on all the lines and just pressed one button that passed all those inputs as a list or dict (there will be more lines added per "shot")

推荐答案

问题是在每次运行时,self.createShotBtnself.labelNameself.shotName 被覆盖.

The problem is that on each run through the values of self.createShotBtn, self.labelName and self.shotName are being overridden.

所以在最后一次运行时,它们是固定的,但仅限于最后一次迭代.

So on the last run through, they are fixed, but only for the last iteration.

相反,您希望在循环中使用局部作用域变量,并可能将其存储在数组中以备后用.

Instead, you want to use a locally scoped variable in the loop, and potentially store it in an array for later use.

这段代码应该接近你所需要的,但我可以看到 self.shotNumberLst(返回一个数字?)和 shotjump(这是一个错误,或等于 i) 被声明.

This code should come close to what you need, but I can see where self.shotNumberLst (which returns a number?) and shotjump (which is an offest, or equal to to i) are declared.

self.shots = []
for i in range(seqNum): # Changed while to for, so you don't need to increment
    #create each widget
    createShotBtn = QtGui.QPushButton("Create Shot %s!" %str(self.shotNumberLst[i-1]))
    labelName = QtGui.QLabel(self)
    labelName.setText("Enter Name Of Shot %s!" %str(self.shotNumberLst[i-1]))
    shotName = QtGui.QLineEdit(self)

    self.shots.append({"button":createShotBtn,
                       "name":shotName)) # Store for later if needed.

    #add widget to layout
    self.grid.addWidget(labelName, 11+shotjump,0)
    self.grid.addWidget(shotName,11+shotjump,1)
    self.grid.addWidget(createShotBtn, 11+shotjump,2)

    #Press button that makes magic happen
    createShotBtn.clicked.connect(self.createShot(i))

#elsewhere
def createShot(self,index):
    print self.shots[index]["name"].text

这篇关于pyside/pyqt:在单击按钮时从动态创建的 qlineedits 中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆