PyQt example: How to run a command and disply its stdout
This widget consists of a QLineEdit
class and a QTextEdit
class. The user enters a DOS command in the input box, hits RETURN, and the stdout from the command is displayed in the text box.
import os
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
def main():
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
# create objects
label = QLabel(self.tr("Enter command and press Return"))
self.le = QLineEdit()
self.te = QTextEdit()
# layout
layout = QVBoxLayout(self)
layout.addWidget(label)
layout.addWidget(self.le)
layout.addWidget(self.te)
self.setLayout(layout)
# create connection
self.connect(self.le, SIGNAL("returnPressed(void)"),
self.run_command)
def run_command(self):
cmd = str(self.le.text())
stdouterr = os.popen4(cmd)[1].read()
self.te.setText(stdouterr)
if __name__ == "__main__":
main()
Related posts
- PyQt: How to pass arguments while emitting a signal — posted 2008-01-29
- PyQt4 QItemDelegate example with QListView and QAbstractListModel — posted 2008-01-23
- How to install pyqt4 on ubuntu linux — posted 2008-01-15
- Python PyQt Tab Completion example — posted 2008-01-04
- How to capture the Tab key press event with PyQt 4.3 — posted 2008-01-03
- PyQt 4.3 Simple QAbstractListModel/ QlistView example — posted 2008-01-03
Comments
Thanks for this concise example
Very Nicely done. If I may request an example of connecting input and output from a keyboard and program through a Qt window using an imported QtDesigner .py file.
Thank you!
disqus:2022044520