wxPython: how to create a bash shell window?

后端 未结 4 837
渐次进展
渐次进展 2020-12-01 15:11

I want to create a popup window using wxPython that acts like a bash shell. I don\'t want a terminal emulator, I don\'t need job control, I just want a REPL (Read, Eval, Pr

4条回答
  •  温柔的废话
    2020-12-01 15:53

    Going to see what i can come up with.

    But if you change your mind and decide to use pygtk instead, here it is:

    enjoy!!

    EDIT

    I started making a poor man's version of a terminal using the text control widget. I stopped because there are flaws that can't be fixed, such as when you use the sudo command.

    import wx
    import subprocess
    
    class MyFrame(wx.Frame):
        def __init__(self, *args, **kwds):
            # begin wxGlade: MyFrame.__init__
            kwds["style"] = wx.DEFAULT_FRAME_STYLE
            wx.Frame.__init__(self, *args, **kwds)
    
            self.prompt = "user@stackOvervlow:~ "
            self.textctrl = wx.TextCtrl(self, -1, '', style=wx.TE_PROCESS_ENTER|wx.TE_MULTILINE)
            self.default_txt = self.textctrl.GetDefaultStyle()
            self.textctrl.AppendText(self.prompt)
    
            self.__set_properties()
            self.__do_layout()
            self.__bind_events()
    
    
        def __bind_events(self):
            self.Bind(wx.EVT_TEXT_ENTER, self.__enter)
    
    
        def __enter(self, e):
            self.value = (self.textctrl.GetValue())
            self.eval_last_line()
            e.Skip()
    
    
        def __set_properties(self):
            self.SetTitle("Poor Man's Terminal")
            self.SetSize((800, 600))
            self.textctrl.SetFocus()
    
        def __do_layout(self):
            sizer_1 = wx.BoxSizer(wx.VERTICAL)
            sizer_1.Add(self.textctrl, 1, wx.EXPAND, 0)
            self.SetSizer(sizer_1)
            self.Layout()
    
        def eval_last_line(self):
            nl = self.textctrl.GetNumberOfLines()
            ln =  self.textctrl.GetLineText(nl-1)
            ln = ln[len(self.prompt):]
            args = ln.split(" ")
    
            proc = subprocess.Popen(args, stdout=subprocess.PIPE)
            retvalue = proc.communicate()[0]
    
            c = wx.Colour(239, 177, 177)
            tc = wx.TextAttr(c)
            self.textctrl.SetDefaultStyle(tc)
            self.textctrl.AppendText(retvalue)
            self.textctrl.SetDefaultStyle(self.default_txt)
            self.textctrl.AppendText(self.prompt)
            self.textctrl.SetInsertionPoint(GetLastPosition() - 1)
    
    if __name__ == "__main__":
        app = wx.PySimpleApp(0)
        wx.InitAllImageHandlers()
        frame_1 = MyFrame(None, -1, "")
        app.SetTopWindow(frame_1)
        frame_1.Show()
        app.MainLoop()
    

    If really wanted, this could be worked upon.

提交回复
热议问题