如何在 PyQt4 中使用 QtGui 将输入变量收集到 python 程序?

How to use QtGui in PyQt4 to collect input variables to a python program?

我不想使用 sys.argv 在命令行上输入标志,而是想更改我的 python 程序,以便带有对话框的 GUI 允许用户提供某些问题的答案。按下 "apply" 按钮后,我希望对话框 GUI 消失,然后使用对话框中提供的变量将代码正常设置为 运行。我使用 QtGui 尝试了许多不同的方法。它要么崩溃,要么主程序不知道答案。下面我展示了我的许多尝试之一。在此示例中,按下应用按钮后答案未知。输出是

after widget answer1 = <blank no matter what I write in the dialog box>

我非常感谢有关如何更改此代码以便变量 "a1" 填充对话框中给出的字符串的帮助。 谢谢!

import sys
from PyQt4 import QtGui

class Widget(QtGui.QWidget):

    def __init__(self):
        super(Widget, self).__init__()

        self.answer1 = QtGui.QLabel()
        q1Edit = QtGui.QLineEdit()
        q1Edit.textChanged[str].connect(self.q1Changed)

        grid = QtGui.QGridLayout()
        grid.setSpacing(20)

        grid.addWidget(QtGui.QLabel('Question 1'), 1, 0)
        grid.addWidget(q1Edit, 1, 1)
        grid.addWidget(self.answer1, 1, 2)

        applyBtn = QtGui.QPushButton('Apply', self)
        applyBtn.clicked.connect(self.close)

        self.answer1Text = self.answer1.text()

        grid.addWidget(applyBtn,4,3)

        self.setLayout(grid)     

        self.setGeometry(300, 300, 350, 300)
        self.show()


    def q1Changed(self, text):
        self.answer1.setText(text)

    def returnAnswer1(self):
        return self.answer1Text


def main():    
    app = QtGui.QApplication(sys.argv)
    w = Widget() 
    a1 = w.returnAnswer1()
    print("after widget answer1 = " + a1)    
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

感谢 JRazor,这是最终的解决方案:

import sys
from PyQt4 import QtGui

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

        self.answer1 = QtGui.QLabel()
        q1Edit = QtGui.QLineEdit()
        q1Edit.textChanged.connect(self.q1Changed)

        self.answer2 = QtGui.QLabel()
        q2Edit = QtGui.QLineEdit()
        q2Edit.textChanged.connect(self.q2Changed)

        grid = QtGui.QGridLayout()
        grid.setSpacing(20)

        grid.addWidget(QtGui.QLabel('Question 1'), 1, 0)
        grid.addWidget(q1Edit, 1, 1)

        grid.addWidget(QtGui.QLabel('Question 2'), 2, 0)
        grid.addWidget(q2Edit, 2, 1)

        applyBtn = QtGui.QPushButton('Apply', self)
        applyBtn.clicked.connect(self.close)

        grid.addWidget(applyBtn,3,2)
        self.setLayout(grid)
        self.setGeometry(300, 300, 350, 300)

    def q1Changed(self, text):
        self.answer1.setText(text)

    def q2Changed(self, text):
        self.answer2.setText(text)

    def returnAnswer1(self):
        return self.answer1.text()

    def returnAnswer2(self):
        return self.answer2.text()

    @staticmethod
    def getData(parent=None):
        dialog = Widget(parent)
        dialog.exec_()
        return [dialog.returnAnswer1(), dialog.returnAnswer2()]

def main():
    app = QtGui.QApplication([])
    window = Widget()
    data = window.getData()
    print data[0]
    print data[1]


if __name__ == '__main__':
    main()

您需要将 QWidget 更改为 QDialog:

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

然后使用静态方法为 class Widget 创建一个函数。函数将从编辑字段中获取对话框 window 和 return 文本的父级:

@staticmethod
def getData(parent = None):
    dialog = Widget(parent)
    dialog.exec_()
    return dialog.returnAnswer1()

然后调用 window 并传递一个值:

def main():
    app = QtGui.QApplication([])
    window = Widget()
    print window.getData()  # window is value from edit field

此外,您需要从 class 中删除 self.show()

完整代码:https://gist.github.com/gitex/83b06e8ceaac18ad5dec2d332ed6cbaa