wxPython + pysftp 不能同时工作

发布于 2024-11-25 17:34:34 字数 731 浏览 4 评论 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 连接到远程设备,并且我希望它在每次发生操作时向状态文本框添加一行。第一个只是连接到主机。但是,我的面板仅在代码连接到主机后才显示,即使用于制作面板等的代码是之前的。

我能做些什么?没有错误,只是这种奇怪的行为。 谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明天过后 2024-12-02 17:34:34

正如已经提到的,这是因为您是在构造函数中执行此操作。

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

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")
遥远的她 2024-12-02 17:34:34

您尝试在面板的构造函数中修改面板,但面板仅在构造函数执行后显示(在 .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")

编辑:添加连接启动的线程启动以避免块显示的阻塞操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文