2015-01-26 88 views
0

我创建了一个布局,一旦我将它分配到列表中,但是为了创建一个用户,我需要线编辑和comboxes中的值,但是会自动添加标签和lineedits你可以看到我已经设置了每个对象名称,我如何访问我的tryCreateUser()函数中的所有值?正如你可以看到我通过打印编辑检查,但只是给了我最后一个对象!提前感谢您的帮助!Pyqt4在创建动态表单时访问对象

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) 

回答

2

你应该让每个按钮自己注册一个字典。试试这个:

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 

然后你有你的东西有一个中央存储库中buttons,并且可以进行迭代。

def tryCreateUser(): 
    for title, button in self.buttons.items(): 
     print("Title is {}, button obj is {}".format(title, button)) 

请注意,这不完全是我的做法,但它是最接近您的原始示例。老实说,那些每个如果块应该是其自身的功能,这样你就会有这样的:

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

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() 
+0

谢谢!这样可行! – Inthuson 2015-01-31 20:25:11