Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    [wxPython] Redirigere uoutput comando su TextCtrl

    salve a tutti!
    il mio programmino esegue un comando di sistema, e quindi dovrei fare in modo che l'output venga rediretto su una TextCtrl per essere visibile.
    ho fatto alcune prove seguendo varie guide, ma senza successo.
    ad esempio:
    codice:
    import subprocess
    import wx
    import sys
    from wx._controls import Button
    
    class Form(wx.Frame):
        
        txtUrl = None
        lblWait = None
    
        def __init__(self):
            wx.Frame.__init__(self, None, title = "Python", size = (410, 400), style = wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
    
            btnGo = Button(self, label = "GO", pos = (5, 40))
            btnGo.Bind(wx.EVT_BUTTON, self.comando)
            
            self.txtResult = wx.TextCtrl(self, pos = (5, 100), size = (400, 300), style = wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL)
    
            redir = RedirectText(self.txtResult)
            sys.stdout = redir
            
        def comando(self, strUrl):
            subprocess.call(["ls", "-lR", "/home/casa/.netbeans"])
                
                
    class RedirectText(object):
        def __init__(self, aWxTextCtrl):
            self.out = aWxTextCtrl
     
        def write(self, string):
            wx.CallAfter(self.out.WriteText, string)
    esegue il comando, ma l'output rimane sul terminale e non viene inviato alla gui.
    sapete dirmi dove sbaglio??

  2. #2
    ho dovuto fare parecchie modifiche:
    codice:
    import subprocess
    import wx
    import sys
    from wx._controls import Button
    
    class Form(wx.Frame):
        
        txtUrl = None
        lblWait = None
    
        def __init__(self):
            wx.Frame.__init__(self, None, title = "Python", size = (410, 400), style = wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
    
            btnGo = Button(self, label = "GO", pos = (5, 40))
            btnGo.Bind(wx.EVT_BUTTON, self.comando)
            
            self.txtResult = wx.TextCtrl(self, pos = (5, 100), size = (400, 300), style = wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL)
    
         def comando(self, strUrl):
            c = subprocess.Popen(["ls", "-lR", "/home/casa/.netbeans"], shell = True, stdout = subprocess.PIPE)
            outThread = RedirectText(c.stdout, self.txtResult.AppendText)
    
    class RedirectText(threading.Thread):
        def __init__(self, stdOut, cb):
            super(RedirectText, self).__init__()
            self.stdOut = stdOut
            self.cb = cb
     
        def run(self):
            text = None
            while text != '':
                text = self.stdOut.readline()
                self.cb(text)
    ho usato i thread e Popen al posto di call.
    diciamo che funziona, tranne per il fatto che Popen mi fa il comando ls sulla cartella dove c'è il file che lancio, e non sul percorso che intendo io.
    ma forse sbaglio io a lanciarlo.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.