2011-07-25 11 views
0

マイコード:wxPythonを+のpysftpが同時に動作しません

だから、
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") 

、私のコードでわかるように、私は自己、「ステータス」テキストボックスとパネルを生成しようとしています。コントロール。私はpysftpを使ってリモートデバイスに接続し、アクションが発生するたびにステータステキストボックスに行を追加したいと考えています。最初のものはホストに接続するだけです。しかし、私のパネルは、のコードがホストに接続された後に表示されます、パネルなどを作るためのコードが以前であっても。

どうすればよいですか?エラーはなく、この奇妙な動作です。 ありがとう!

答えて

1

すでに述べたように、これはコンストラクタでこれを行うためです。

使用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") 
+0

ありがとう!これはそれでした。 –

0

パネルのコンストラクタでパネルを変更しようとしていますが、パネルはコンストラクタの実行後に表示されます(.MainLoop()および/または.Show()のコールの後)。

これを行うための正しい方法は本

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ような何かのイベント(cf doc)のハンドラを登録することである:ブロック表示thaztブロック操作を避けるために、接続開始のためのスレッド打ち上げを追加しました。

+0

おかげで、それは事を変えませんでした。両方のメッセージが引き続き表示されるため、イベントが機能しているように見えます。ただし、接続が完了した後もウィンドウが表示されます。 –

+0

@Judge John Deed:私は自分の答えを編集して、GUIをブロック解除するためのスレッド方式で接続を開始しました。 –

+0

あなたの時間はMerciですが、@欠けている方法はずっと簡単です。しかしもう一度ありがとう。 –

関連する問題