Python:具有多个QLineEdit框(pyqt4)

Python:具有多个QLineEdit框(pyqt4),python,qlineedit,Python,Qlineedit,我想有2个输入文本框彼此相邻 我试图复制/跳过一个文本框的代码,但没有得到正确的结果。仅显示一个文本框 如果可能的话,我想让标签与文本框对齐 class UI_central(QtGui.QDialog): def __init__(self, parent=None): super(UI_central, self).__init__(parent) """ Input box for stock name """ label1 = QtGui.QL

我想有2个输入文本框彼此相邻

我试图复制/跳过一个文本框的代码,但没有得到正确的结果。仅显示一个文本框

如果可能的话,我想让标签与文本框对齐

class UI_central(QtGui.QDialog):

def __init__(self, parent=None):
    super(UI_central, self).__init__(parent)

    """
    Input box for stock name
    """
    label1 = QtGui.QLabel('Stock', self)
    label1.move(50, 0)

    self.line_edit = QtGui.QLineEdit()
    self.line_edit.setText("Stock name")

    hbox = QtGui.QHBoxLayout()
    hbox.addWidget(self.line_edit)
    self.setLayout(hbox)

    """
    Input box for stock amount
    """
    label2 = QtGui.QLabel('How Many?', self)
    label2.move(100, 0)

    self.line_edit2 = QtGui.QLineEdit()
    self.line_edit2.setText("Stock amount")

    hbox2 = QtGui.QHBoxLayout()
    hbox2.addWidget(self.line_edit2)
    self.setLayout(hbox2)        

    """
    Push buttons
    """
    submit_button = QtGui.QPushButton("Submit")
    clear_button = QtGui.QPushButton("Clear")

    hbox.addWidget(submit_button)
    hbox.addWidget(clear_button)

    self.connect(submit_button, QtCore.SIGNAL("clicked()"),
                 self.submit)

    self.connect(clear_button, QtCore.SIGNAL("clicked()"),
                 self.clear)
    return

def submit(self):
    str = self.line_edit.text()
    # check str before doing anything with it!
    print(str)

def clear(self):
    print ("cleared")
    self.line_edit.setText("")

您的代码创建了两个线条编辑,但存在布局问题。每个窗口只能有一个布局管理器。第二次调用setLayoutself将删除第一个布局管理器

顺便说一句,您可以将一个布局管理器嵌套在另一个布局管理器中。为此,BoxLayout管理器有一个函数addLayout

另外,我不知道当你混合调用布局管理器移动时会发生什么。我总是让布局经理负责所有孩子的定位

我删除了第二个布局,现在两个线条编辑都应该出现

class UI_central(QtGui.QDialog):
    def __init__(self, parent=None):
        super(UI_central, self).__init__(parent)

        """
        Input box for stock name
        """
        label1 = QtGui.QLabel('Stock', self)
        label1.move(50, 0)

        self.line_edit = QtGui.QLineEdit()
        self.line_edit.setText("Stock name")

        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.line_edit)
        self.setLayout(hbox)

        """
        Input box for stock amount
        """
        label2 = QtGui.QLabel('How Many?', self)
        label2.move(100, 0)

        self.line_edit2 = QtGui.QLineEdit()
        self.line_edit2.setText("Stock amount")

        hbox.addWidget(self.line_edit2)

        """
        Push buttons
        """
        submit_button = QtGui.QPushButton("Submit")
        clear_button = QtGui.QPushButton("Clear")

        hbox.addWidget(submit_button)
        hbox.addWidget(clear_button)

        self.connect(submit_button, QtCore.SIGNAL("clicked()"),
                 self.submit)

        self.connect(clear_button, QtCore.SIGNAL("clicked()"),
                 self.clear)

def submit(self):
    str = self.line_edit.text()
    # check str before doing anything with it!
    print(str)

def clear(self):
    print ("cleared")
    self.line_edit.setText("")

谢谢@Paul Cornelius!!您知道如何将标签移动到输入框的左侧吗?您可以为每个标签/输入对设置单独的HBoxLayout。首先添加标签,然后添加输入,使标签位于左侧。然后,您可以为整个窗口设置一个主布局管理器,并使用addLayout功能将每个标签/输入布局添加到主布局中。addLayout和addWidget函数还使用标志来控制定位和重量。一旦你掌握了窍门,它就会变得非常灵活。你太棒了!谢谢