Python - PyQT5打印输出QTextEdit内部

时间:2018-03-12 18:46:02

标签: python-3.x pyqt5

我有一个pyqt5代码来打印QTextEdit

中的字典输出
#code:-
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(565, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.text_output = QtWidgets.QTextEdit(self.centralwidget)
        self.text_output.setGeometry(QtCore.QRect(20, 10, 501, 251))
        self.text_output.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        self.text_output.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        self.text_output.setObjectName("text_output")
        self.btn_dict = QtWidgets.QPushButton(self.centralwidget)
        self.btn_dict.setGeometry(QtCore.QRect(30, 280, 101, 21))
        self.btn_dict.setObjectName("btn_dict")
        self.btn_clear = QtWidgets.QPushButton(self.centralwidget)
        self.btn_clear.setGeometry(QtCore.QRect(180, 280, 101, 21))
        self.btn_clear.setObjectName("btn_clear")
        self.btn_exit = QtWidgets.QPushButton(self.centralwidget)
        self.btn_exit.setGeometry(QtCore.QRect(310, 280, 101, 21))
        self.btn_exit.setObjectName("btn_exit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 565, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.btn_dict.setText(_translate("MainWindow", "PrintDictionary"))
        self.btn_clear.setText(_translate("MainWindow", "Clear"))
        self.btn_exit.setText(_translate("MainWindow", "Exit"))

    def dictionary(self):
        new_dict = {
              'Name' : 'Ron',
              'Age' : '21',
              'city' : 'NY'
}

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

因此,我不想在控制台上打印输出,而是在点击PrintDictionary按钮时想要输出QTextEdit内的输出,以及在点击退出按钮时单击清除按钮或退出应用程序时如何清除QTextEdit

我是PYQT5的新手,所以请帮帮我。 感谢

1 个答案:

答案 0 :(得分:0)

首先,dictionary函数不一定必须是该类的成员,并且因为您没有将self传递给它,所以我假设您在该部分中严重缩进了代码。

说到这一点,您必须使用单击的信号并调用lambda函数,该函数通过QTextEdit的append和clear方法执行操作。

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        [...]

    def dictionary(self):
        new_dict = {
            'Name' : 'Ron',
            'Age' : '21',
            'city' : 'NY'
        }
        return new_dict

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    ui.btn_dict.clicked.connect(lambda: ui.text_output.append(str(ui.dictionary())))
    ui.btn_clear.clicked.connect(ui.text_output.clear)
    MainWindow.show()
    sys.exit(app.exec_())