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")
所以,从我的代码可以看出,我正在尝试生成一个带有“状态”文本框 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如已经提到的,这是因为您是在构造函数中执行此操作。
使用 wx.CallAfter:
As already mentioned, it is because you are doing this in the constructor.
Use wx.CallAfter:
您尝试在面板的构造函数中修改面板,但面板仅在构造函数执行后显示(在
.MainLoop()
和/或.Show() 之后的某个位置)
调用)。执行此操作的正确方法是通过使用类似以下内容注册事件处理程序(cf doc)
编辑:添加连接启动的线程启动以避免块显示的阻塞操作。
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
EDIT: Added a threaded launch for the connection starting to avoid a blocking operation thazt block display.