开发者

wxPython + pysftp won't work at the same time

My code:

class ConnectingPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, pare开发者_如何学JAVAnt)
        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!


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")


You're trying to modify your panel in the constructor of the panel, but the panel is only displayed after the constructor execution (somewhere after the .MainLoop() and/or .Show() call).

The correct way of doing this is by registering handlers for events (cf doc) with something like this

import  wx.lib.newevent
import threading

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.MyNewEvent, self.EVT_MY_NEW_EVENT = wx.lib.newevent.NewEvent()
        self.Bind(self.EVT_MY_NEW_EVENT, self.connected_handler) # you'll have to find a correct event
        thread = threading.Thread(target=self.start_connection)
        thread.start()

    def start_connection(self):
        self.control.AppendText("Connecting to device")
        self.device = Connection(#info goes here)
        evt = self.MyNewEvent()
        #post the event
        wx.PostEvent(self, evt)

    def connected_handler(self, event):
        self.control.AppendText("Connected to device")

EDIT: Added a threaded launch for the connection starting to avoid a blocking operation thazt block display.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜