简体   繁体   English

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

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

Instead of input flags on the command line, using sys.argv, I want to change my python program so that a GUI with dialog boxes lets users provide answers to some questions. 我想使用sys.argv而不是在命令行上输入标志,而是要更改python程序,以便带有对话框的GUI可以使用户提供一些问题的答案。 After the "apply" button is pushed I want the dialog GUI to dissapear and then the code to run normally with the variables provided in dialog boxes. 按下“应用”按钮后,我希望对话框GUI消失,然后使用对话框中提供的变量正常运行代码。 I have tried many different ways using QtGui. 我已经尝试过使用QtGui的许多不同方式。 Either it crashes, or, the answers are not known for the main program. 它要么崩溃,要么主程序不知道答案。 Below I show one of my many attempts. 下面我展示了我的许多尝试之一。 In this example the answer is not known after the apply button is pressed. 在此示例中,按下“应用”按钮后答案未知。 The output is 输出是

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

I would very much appreciate help on how to change this code so that the variable "a1" is filled with the string given in the dialog box. 我非常感谢您提供有关如何更改此代码的帮助,以使变量“ a1”充满对话框中给定的字符串。 Thank you! 谢谢!

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

Thanks to JRazor, this was in the end the solution: 多亏了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()

You need change QWidget to QDialog : 您需要将QWidget更改为QDialog

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

Then create a function for class Widget with a static method. 然后使用静态方法为class Widget创建一个函数。 Function will get parent to dialog window and return text from edit field: 函数将使父级进入对话框窗口并从编辑字段返回文本:

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

Then call the window with passing a value: 然后通过传递值来调用窗口:

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

Also, you need delete self.show() from class. 另外,您需要从类中删除self.show()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM