将打印重定向到状态栏
是否可以将文本重定向到状态栏。这里我想显示打印到状态栏。
它应该看起来像这样,但它不起作用:
import sys
import wx
class RedirectText:
def __init__(self, statusbar):
self.statusbar = statusbar
def write(self,string):
self.statusbar.SetStatusText(string)
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.log = wx.TextCtrl(self, -1, '', style=wx.TE_READONLY|wx.TE_MULTILINE)
sizer = wx.BoxSizer()
sizer.Add(self.log, 1, wx.ALL | wx.EXPAND, 5)
self.SetSizer(sizer)
self.statusbar = self.CreateStatusBar()
redirection = RedirectText(self.statusbar)
sys.stdout = redirection
print 'hello'
self.Bind(wx.EVT_CLOSE, self.OnClose)
def OnClose(self, event):
raise RuntimeError('error')
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MainFrame()
frame.Show()
app.MainLoop()
谢谢
Is it possible to redirect text to status bar. Here I want to display a print to status bar.
It should look like this but it doesn't work:
import sys
import wx
class RedirectText:
def __init__(self, statusbar):
self.statusbar = statusbar
def write(self,string):
self.statusbar.SetStatusText(string)
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.log = wx.TextCtrl(self, -1, '', style=wx.TE_READONLY|wx.TE_MULTILINE)
sizer = wx.BoxSizer()
sizer.Add(self.log, 1, wx.ALL | wx.EXPAND, 5)
self.SetSizer(sizer)
self.statusbar = self.CreateStatusBar()
redirection = RedirectText(self.statusbar)
sys.stdout = redirection
print 'hello'
self.Bind(wx.EVT_CLOSE, self.OnClose)
def OnClose(self, event):
raise RuntimeError('error')
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MainFrame()
frame.Show()
app.MainLoop()
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能有点出乎意料,但是您定义的 write 函数将在当前代码中被调用两次:首先它将接收“hello”字符串,然后是换行符。由于换行符在状态栏中不可见,因此看起来没有任何更新。
一个简单的解决方法是检查
write
函数中string
的内容,看看它是否包含任何数据:This might be a bit unexpected, but the
write
function you defined will get called twice in your current code: first it will receive the "hello" string, then a newline. Because the newline isn't visible in your statusbar, it looks like nothing was updated.An easy fix is to check the contents of
string
in yourwrite
function, and see if it contains any data:@jro是对的。
print
正在通过 stdout 分两次发送"hello\n"
。然后,另一个更简单的修复(尽管可能不太方便)是用逗号结束
print
:有效,因为逗号告诉
print
保持在同一行。@jro is right.
print
is sending"hello\n"
in two shots through stdout.Then, another still easier fix (despite maybe less convenient) is to end your
print
s with a comma:works because the comma tells
print
to stay in the same line.