我试图在同一个窗口中显示编辑文本中的用户输入文本,我看到你可以在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_()
答案 0 :(得分:2)
您可以使用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_())