0

I'm new to PyQT and I'm looking for a code that demonstrates a simple push button, which when clicked will open a new small window with QTextEdit in it

Zed
  • 5,683
  • 11
  • 49
  • 81
  • 1
    http://stackoverflow.com/a/4839224/1110381 ... – l4mpi Jan 19 '13 at 01:31
  • well, I tried w1 = QTextEdit() w1.show() but nothing happens – Zed Jan 19 '13 at 10:44
  • You need to construct a QApplication before this, and if you're not running interactively call _exec on the QApp to make sure the program doesn't just exit immediately. But the reason I linked this specific answer was the comment about modal dialogs: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qinputdialog.html#details – l4mpi Jan 19 '13 at 12:03

1 Answers1

13

Here is something to start with:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtCore, QtGui

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

        self.buttonBox = QtGui.QDialogButtonBox(self)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)

        self.textBrowser = QtGui.QTextBrowser(self)
        self.textBrowser.append("This is a QTextBrowser!")

        self.verticalLayout = QtGui.QVBoxLayout(self)
        self.verticalLayout.addWidget(self.textBrowser)
        self.verticalLayout.addWidget(self.buttonBox)

class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.pushButtonWindow = QtGui.QPushButton(self)
        self.pushButtonWindow.setText("Click Me!")
        self.pushButtonWindow.clicked.connect(self.on_pushButton_clicked)

        self.layout = QtGui.QHBoxLayout(self)
        self.layout.addWidget(self.pushButtonWindow)

        self.dialogTextBrowser = MyDialog(self)

    @QtCore.pyqtSlot()
    def on_pushButton_clicked(self):
        self.dialogTextBrowser.exec_()


if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()

    sys.exit(app.exec_())