Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pyqt4在创建动态表单时访问对象_Python_Oop_Object_Pyqt4 - Fatal编程技术网

Python Pyqt4在创建动态表单时访问对象

Python Pyqt4在创建动态表单时访问对象,python,oop,object,pyqt4,Python,Oop,Object,Pyqt4,我创建了一个布局,一旦我将其分配到列表中,它会自动添加标签和线条编辑,但是对于我创建用户来说,我需要线条编辑和组合框中的值,但是正如您所看到的,我已经设置了每个对象的名称,我如何访问tryCreateUser()函数中的所有值?正如你所看到的,我通过打印编辑进行了检查,但这只给了我最后一个对象!!提前谢谢你的帮助 def addNewUser(self): def tryCreateUser(): print(edit) self.deleteLayout(se

我创建了一个布局,一旦我将其分配到列表中,它会自动添加标签和线条编辑,但是对于我创建用户来说,我需要线条编辑和组合框中的值,但是正如您所看到的,我已经设置了每个对象的名称,我如何访问tryCreateUser()函数中的所有值?正如你所看到的,我通过打印编辑进行了检查,但这只给了我最后一个对象!!提前谢谢你的帮助

def addNewUser(self):
    def tryCreateUser():
        print(edit)

    self.deleteLayout(self.dynamicFrame.layout())

    grid = QtGui.QGridLayout()
    grid.setSpacing(10)
    row = 1
    column = 1
    edit = ['First Name', 1, 'Last Name', 1, 'Date of Birth', 2, '', 'Gender', 3, 'Access Level', 4, 'Password', 5, 'Verify Password', 5]

    for item in edit:
        if item == '':
            self.dob = QtGui.QLabel()
            grid.addWidget(self.dob, 3, 3)
            column -= 1         
        else:
            if item == 1:
                edit = QtGui.QLineEdit()
            elif item == 2:
                edit = QtGui.QPushButton('Choose')
                edit.clicked.connect(self.openCal)
            elif item == 3:
                edit = QtGui.QComboBox()
                edit.addItem('Male')
                edit.addItem('Female')
            elif item == 4:
                edit = QtGui.QComboBox()
                edit.addItem('General Staff')
                edit.addItem('Stock Admin')
                edit.addItem('Manager')
            elif item == 5:
                edit = QtGui.QLineEdit()
                edit.setEchoMode(QtGui.QLineEdit.Password)
            else:
                edit = QtGui.QLabel(item)

        edit.setObjectName(str(item))
        grid.addWidget(edit, row, column)
        if column >= 2:
            column = 1
            row += 1
        else:
            column += 1

    createButton = QtGui.QPushButton("Create User")
    createButton.clicked.connect(tryCreateUser)
    cancelButton = QtGui.QPushButton("Cancel")
    cancelButton.clicked.connect(self.populateUser)
    grid.addWidget(createButton, row , column)
    grid.addWidget(cancelButton, row, (column+1))

    self.dynamicFrame.setLayout(grid)

你应该让每个按钮在某个地方注册一本字典。请尝试以下方法:

from collections import namedtuple

Button = namedtuple('Button', ["title", "to_do"])

buttons_to_make = [Button("First Name", [1]),
                   Button("Last Name", [1]),
                   Button("Date of Birth", [2, '']),
                   ...]

self.buttons = {}
for button in buttons_to_make:
    for action in button.to_do:
        # your big long if/elif structure here, e.g.:
        if action == 1:
            b = QtGui.QLineEdit()
        elif action == 2:
            b = QtGui.QPushButton('Choose')
            b.clicked.connect(self.openCal)
        # etc
    self.buttons[button.title] = b
然后,在
按钮
中有一个所有内容的中央存储库,可以对其进行迭代

def tryCreateUser():
    for title, button in self.buttons.items():
        print("Title is {}, button obj is {}".format(title, button))
请注意,这并不是我想要的方法,但它最接近您的原始示例。老实说,每一个if块都应该是它自己的函数,这样您就可以得到如下结果:

def __create_QLineEdit(self):
    b = QtGui.QLineEdit()
    return b

def __create_QPushButton(self):
    b = QtGui.QPushButton("Choose")
    b.clicked.connect(self.openCal)
    self.dob = QtGui.QLabel()
    grid.addWidget(self.dob, 3, 3)
    column -= 1
    return b

...
然后,将namedtuple定义为:

Button = namedtuple("Button", ['name','action'])
并将
按钮定义为

buttons_to_make = [Button("First Name", self.__create_QLineEdit),
                   Button("Last Name", self.__create_QLineEdit),
                   Button("Date of Birth", self.__create_QPushButton),
                   ...]
并以以下方式执行:

buttons = {}
for button in buttons_to_make:
    name, action = button
    buttons[name] = action()