wxPython:如何创建一个bash shell窗口?

Bry*_*ley 9 wxpython

我想使用wxPython创建一个弹出窗口,其作用类似于bash shell.我不想要终端模拟器,我不需要作业控制,我只想要一个基于bash过程的REPL(读取,评估,打印循环).

使用wxPython有一个简单的方法吗?我知道我作为一名tcl/tk程序员的基本概念,但我的wxPython fu很弱,如果我不需要,我不想重新发明轮子.我已经阅读了一些关于py.shell的内容.Shell,但看起来它创建了一个python shell,我想要一个运行bash命令.

Anu*_*yal 7

好的,这是另一个尝试,它在一个单独的线程中读取所有输出和错误,并通过队列进行通信.我知道这不是完美的(例如,延迟输出的命令将无法工作,并且输出将进入下一个提交,例如tryr sleep 1; date)并且复制整个bash并非琐碎但是对于我测试的几个命令它似乎工作正常

关于wx.py.shell的API,我刚刚实现了Shell类为Interpreter调用的那些方法,如果你通过Shell的源代码,你就会明白.基本上

  • push是用户输入命令发送给解释器的地方
  • getAutoCompleteKeys返回用户可以用来自动完成命令的键,例如tab键
  • getAutoCompleteList返回给定文本的命令匹配列表

  • getCallTip"在弹出窗口中显示参数规范和docstring.所以对于bash,我们可能会显示手册页:)

这是源代码

import threading
import Queue
import time

import wx
import wx.py
from subprocess import Popen, PIPE

class BashProcessThread(threading.Thread):
    def __init__(self, readlineFunc):
        threading.Thread.__init__(self)

        self.readlineFunc = readlineFunc
        self.outputQueue = Queue.Queue()
        self.setDaemon(True)

    def run(self):
        while True:
            line = self.readlineFunc()
            self.outputQueue.put(line)

    def getOutput(self):
        """ called from other thread """
        lines = []
        while True:
            try:
                line = self.outputQueue.get_nowait()
                lines.append(line)
            except Queue.Empty:
                break
        return ''.join(lines)

class MyInterpretor(object):
    def __init__(self, locals, rawin, stdin, stdout, stderr):
        self.introText = "Welcome to stackoverflow bash shell"
        self.locals = locals
        self.revision = 1.0
        self.rawin = rawin
        self.stdin = stdin
        self.stdout = stdout
        self.stderr = stderr

        self.more = False

        # bash process
        self.bp = Popen('bash', shell=False, stdout=PIPE, stdin=PIPE, stderr=PIPE)

        # start output grab thread
        self.outputThread = BashProcessThread(self.bp.stdout.readline)
        self.outputThread.start()

        # start err grab thread
        self.errorThread = BashProcessThread(self.bp.stderr.readline)
        self.errorThread.start()

    def getAutoCompleteKeys(self):
        return [ord('\t')]

    def getAutoCompleteList(self, *args, **kwargs):
        return []

    def getCallTip(self, command):
        return ""

    def push(self, command):
        command = command.strip()
        if not command: return

        self.bp.stdin.write(command+"\n")
        # wait a bit
        time.sleep(.1)

        # print output
        self.stdout.write(self.outputThread.getOutput())

        # print error
        self.stderr.write(self.errorThread.getOutput())

app = wx.PySimpleApp()
frame = wx.py.shell.ShellFrame(InterpClass=MyInterpretor)
frame.Show()
app.SetTopWindow(frame)
app.MainLoop()
Run Code Online (Sandbox Code Playgroud)


Bry*_*ley 2

我找到了解决问题的方法。有趣的是,它以前从未出现在谷歌搜索中。它不是生产就绪的代码,但最终,我正在寻找一种在 wxPython 窗口中运行 bash shell 的方法。

网络存档上的 http://sivachandran.blogspot.com/2008/04/termemulator-10-released.html https://sourceforge.net/projects/termemulator/files/TermEmulator/1.0/