我正在PySide中创建一个应用程序,我想添加一个控制台/终端屏幕,在这里你有一个提示符,你可以输入命令。我将如何能够做到这一点。我猜测输出的QPlainTextEdit/QTextEdit和实际提示的QLineEdit的组合。有一个更好的方法吗?终端在PySide中的应用程序
3
A
回答
0
你可以看看Spyder。他们使用PyQt(这是相似的),并有一个终端。我想你可以导入他们的终端小部件,但我没有玩过它。
https://code.google.com/p/spyderlib/
而且,这是我最喜欢的Python编辑迄今为止!
我花了一段时间试图找到像这样的东西,但无济于事。祝你好运!
0
我用一个自定义的QPlainTextEdit和自定义的QLineEdit来做到这一点。我还有一个指示标签,可以在终端上显示“>>>”以显示用户输入。它需要更多的工作。最好的方法是创建基于QTextEdit和您自己的io处理程序的自定义小部件。下面是我的执行方法的一个例子,self.input是QLineEdit,self.view是QTextEdit。它应该为你提供一般的想法。
import io, subprocess, shlex, code, rlcompleter, platform
def execute(self, currentText=None):
"""Execute runs the command given based on the console type.
If the console type is "both" then execute will run python commands
unless the user give the input ::os::command or ("::(platform.syste())::command").
Otherwise the console type will determine the what the input will execute with.
Args:
currentText(str): Text to execute. None will run the text from the QLineEdit self.input.
"""
# Check for given text
if currentText is None:
currentText = self.input.text()
self.input.clear()
self.view.display(currentText, "input")
else:
cmd = re.search("^>>>", currentText) # search for start only
if cmd is None:
currentText = ">>>" + currentText
else:
self.view.display(currentText, "input")
# end
# Variables
self.completer.clear()
cmdString = re.sub("^>>>", "", currentText)
result = None
displayType = "output"
run, cmdString = self.getRunType(cmdString)
try:
# Check where the output is going
sys.stdout = self.out = io.StringIO()
sys.stderr = sys.stdout
if run == "python": # Run python command
result = self._runInterpreter(cmdString)
displayType = "python"
elif run == "os": # Run os command
result = self._runSubprocess(cmdString)
displayType = "os"
except Exception as err:
result = str(err)
displayType = "Error"
notFoundPython = "NameError:" in result and "is not defined" in result
notFoundWindows = "returned non-zero exit status" in result
if notFoundPython or notFoundWindows:
result = "Command not found"
finally:
sys.stdout = self.old_stdout
sys.stderr = self.old_stdout
self.display(result, displayType)
# end execute
def getRunType(self, cmdString):
run = self._consoleType
# Check the run type
if self._consoleType == "both":
if re.search("^::python::", cmdString) is not None:
cmdString = re.sub("^::[a-z]*::", "", cmdString)
run = "python"
elif re.search("^(::os::|::"+platform.system()+"::)", cmdString) is not None:
cmdString = re.sub("^::[a-z]*::", "", cmdString)
run = "os"
else:
run = "python"
# end
return run, cmdString
# end getRunType
def _runInterpreter(self, cmdString, outIO=None, run=None):
# Check for a proper console type
if(self._consoleType != "both" and self._consoleType != "python"):
return
# Get the IO
if outIO is None:
outIO = sys.stdout
# Run python command
self.interpreter.push(cmdString)
# Check outIO
result = "Unreadable buffer: Check python's sys.stdout"
if isinstance(outIO, io.StringIO):
result = outIO.getvalue()
else:
if outIO.readable():
result = str(outIO.readlines())
# Check for error
if re.search("^Traceback", result) or re.search("[a-zA-z]*Error:", result):
raise ValueError(result)
return result
# end _runInterpreter
def _runSubprocess(self, cmdString, run=None):
# Check for a proper console type
if(self._consoleType != "both" and self._consoleType != "os"):
return
# Run OS command
cmd = shlex.split(cmdString)
result = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT).decode("utf-8")
# Check for error
if re.search("^Traceback", result) or re.search("[a-zA-z]*Error:", result):
raise ValueError(result)
return result
# end _runSubprocess
相关问题
- 1. qt GUI应用程序中的终端
- 2. Android从终端终止应用程序
- 3. 应用程序不显示在终端
- 4. 占用整个终端的终端应用程序
- 5. 使用整个终端窗口的终端应用程序
- 6. 在bash中显示术语时终止终端应用程序
- 7. 独立的python终端应用程序
- 8. 在QT应用程序中嵌入应用程序(本例中为终端)
- 9. BB10 - Cascades应用程序 - 控制台/终端应用程序
- 10. Perl多终端应用程序
- 11. 通过终端停止应用程序
- 12. 部署终端库Java应用程序
- 13. Mac终端Chrome应用程序.localized
- 14. 从应用程序启动终端
- 15. 如何在终端的ruby应用程序中做文本框?
- 16. 在java web应用程序的网页中访问linux终端
- 17. 在我的应用程序中放置一个'终端'?
- 18. 在应用程序的终端中运行命令
- 19. Mac/Cocoa:在我的应用程序中嵌入终端窗口
- 20. 如何在Ubuntu的终端中自动运行应用程序?
- 21. 如何在Ruby终端应用程序中使用cbreak模式?
- 22. 使用QProcess测试PySide应用程序
- 23. 如何在Java GUI应用程序中读取终端流?
- 24. 我无法获取QTCaptureSession在终端应用程序中捕获
- 25. 在jsp应用程序中执行终端命令
- 26. 如何在Linux终端应用程序中检测XOFF和XON
- 27. 在OSX应用程序中运行终端命令?
- 28. 如何在WPF应用程序中托管终端会话(mstsc)?
- 29. 在可可应用程序中通过终端讲话
- 30. 如何在Android应用程序中运行终端命令?