Search code examples
pythonsshconnectionwxpythonpysftp

wxPython + pysftp won't work at the same time


My code:

class ConnectingPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE, pos=(-2, -2), size=(387, 267))
        self.control.SetForegroundColour((34,139,34))
        self.control.SetBackgroundColour((0,0,0))
        self.control.Disable()

        self.control.AppendText("Connecting to device")
        self.device = Connection(#info goes here)   
        self.control.AppendText("Connected to device")

So, as can be seen by my code, I'm trying to generate a panel with a "status" textbox, self.control. The idea is that I'm connecting to a remote device using pysftp, and that I want it to add a line to the status textbox each time an action takes place. The first one is just connecting to the host. However, my panel only displays once the code has connected to the host, even though the code for making the panel, etc is before.

What can I do? No errors, just this weird behaviour. Thanks!


Solution

  • As already mentioned, it is because you are doing this in the constructor.

    Use wx.CallAfter:

    class ConnectingPanel(wx.Panel):
    
        def __init__(self, parent):
            wx.Panel.__init__(self, parent)
            self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE, pos=(-2, -2), size=(387, 267))
            self.control.SetForegroundColour((34,139,34))
            self.control.SetBackgroundColour((0,0,0))
            self.control.Disable()
    
            wx.CallAfter(self.start_connection)
    
        def start_connection(self):
            self.control.AppendText("Connecting to device")
            self.device = Connection(#info goes here) 
            self.control.AppendText("Connected to device")