0

私のコード:

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

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

私に何ができる?エラーはありません。この奇妙な動作だけです。ありがとう!

4

2 に答える 2

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")
于 2011-07-25T11:36:09.510 に答える
0

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

これを行う正しい方法は、イベントのハンドラー(cf doc)を次のようなものに登録することです。

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

編集:ブロック操作thaztブロック表示を回避し始める接続のスレッド起動を追加しました。

于 2011-07-25T09:50:18.030 に答える