简体   繁体   English

在同一PyQt4窗口上显示用户输入的文本

[英]displaying user entered text on same PyQt4 Window

I am trying to display user entered text in edit text on the same window, I see that you can display the text on QMessageBox as it asks for the parameters to display here is the code for displaying on Qmessagebox how do I display it on the PyQt window. 我试图在同一窗口上的编辑文本中显示用户输入的文本,我看到您可以在QMessageBox上显示文本,因为它要求显示的参数是在Qmessagebox上显示的代码如何在PyQt上显示窗口。

from PyQt4.QtCore import *
from PyQt4.QtGui import *


class AppForm(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.create_main_frame()

    def create_main_frame(self):
        page = QWidget()

        self.button = QPushButton('joy', page)
        self.edit1 = QLineEdit()
        self.edit2 = QLineEdit()

        vbox1 = QVBoxLayout()
        vbox1.addWidget(self.edit1)
        vbox1.addWidget(self.edit2)
        vbox1.addWidget(self.button)
        page.setLayout(vbox1)
        self.setCentralWidget(page)

        self.connect(self.button, SIGNAL("clicked()"), self.clicked)

    def clicked(self):
        QMessageBox.about(self, "My message box", "Text1 = %s, Text2 = %s" % (
            self.edit1.text(), self.edit2.text()))




if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = AppForm()
    form.show()
    app.exec_()

You can use TextEdit to accomplish what you are trying to do, it can be set to readonly as well. 您可以使用TextEdit完成您想做的事情,也可以将其设置为只读。

import sys
from PyQt4 import QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        layout = QtGui.QVBoxLayout(self)
        self.button = QtGui.QPushButton('button')
        self.textedit = QtGui.QTextEdit()
        self.textedit.setReadOnly(True)
        layout.addWidget(self.textedit)
        layout.addWidget(self.button)
        self.button.clicked.connect(self.testing)

    def testing(self):
        self.textedit.append('hello world')

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())

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

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