我想将一个嵌入xterm
小pyqt4
部件并与之通信。特别是我希望能够打印到它并在其上执行命令(这样它在执行命令后返回到正常的用户提示符,就像普通的 shell 一样)。考虑以下最小示例。我怎样才能让它工作?
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class embedxterm(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setMinimumWidth(900)
self.setMinimumHeight(400)
self.process = QProcess(self)
self.terminal = QWidget(self)
self.terminal.setMinimumHeight(300)
self.cmd1 = QPushButton('Command1',self)
self.cmd2 = QPushButton('Command2',self)
self.hello = QPushButton('Print Hello World',self)
layout = QVBoxLayout(self)
layoutH = QHBoxLayout(self)
layoutH.addWidget(self.cmd1)
layoutH.addWidget(self.cmd2)
layoutH.addWidget(self.hello)
layout.addLayout(layoutH)
layout.addWidget(self.terminal)
self.process.start(
'xterm',['-into', str(self.terminal.winId())])
self.cmd1.clicked.connect(self.Ccmd1)
self.cmd2.clicked.connect(self.Ccmd2)
self.hello.clicked.connect(self.Chello)
def Ccmd1(self):
self.process.write('ls -l')
# Should execute ls -l on this terminal
def Ccmd2(self):
self.process.write('ping www.google.com')
# should execute ping www.google.com on this terminal
def Chello(self):
self.process.write('Hello World')
# should just print "Hello World" on this terminal
if __name__ == "__main__":
app = QApplication(sys.argv)
main = embedxterm()
main.show()
sys.exit(app.exec_())