将GUI条目传递到IPython控制台

时间:2018-11-02 14:43:10

标签: python user-interface pyqt5

我对GUI编程经验还很陌生,因此,如果这个答案对某些人来说似乎很简单,我深表歉意,但是我没有找到任何答案。

我希望能够进一步与用户在IPython控制台中传递给GUI的任何输入进行交互,就像我编写一个简单脚本一样。例如,如果我说

a = 2+2

这在变量浏览器中返回(我使用Spyder),如果我键入a,它将返回4。更不用说我可以使用该变量进行大量操作了。

如何在GUI编程中执行此操作。下面是一个简化的PyQt5小部件,带有用户文本输入和一个按钮,该按钮在单击时可打印输入到控制台的文本。但是,如何在控制台中存储该变量,以便进一步使用它?还是我以错误的方式思考这个问题?预先感谢您的帮助。

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(160, 180, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.getText)

        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(150, 80, 113, 20))
        self.lineEdit.setObjectName("lineEdit")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.pushButton.setText(_translate("Dialog", "Save Text"))

    def getText(self):
        user_text = self.lineEdit.text()
        print(user_text)


if __name__ == "__main__":
    import sys
    if QtCore.QCoreApplication.instance() != None:
        app = QtCore.QCoreApplication.instance()
    else:
        app = QtCore.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()

0 个答案:

没有答案