我只是碰到这个自己。 我解决了该问题,同时保留了突出显示条目和通过在UI子类中定义内部打印功能来使用滚动条的功能。
import wx,sys
class RedirectText(object):
def __init__(self,wxTextCtrl):
self.out=wxTextCtrl
def write(self,string):
self.out.WriteText(string)
class SomeGUI(wx.Frame):
def __init__(self,parent,title):
super(SomeGUI,self).__init__(parent,title=title)
self.mainsizer=wx.GridBagSizer(2,2)
self.textout=wx.TextCtrl(self,size=(-1,80), style=wx.TE_MULTILINE|wx.TE_READONLY)
self.mainsizer.Add(self.textout,(0,0),span=wx.GBSpan(1,3),flag=wx.EXPAND)
self.redir=RedirectText(self.textout)
sys.stdout=self.redir
self.buttons=[wx.Button(self, label=val) for val in ['Bob Dole', 'Batman', 'Pet Rock']]
for i,v in enumerate(self.buttons):
self.mainsizer.Add(v,(1,i),flag=wx.EXPAND)
self.Bind(wx.EVT_BUTTON,self.BobDole,self.buttons[0])
self.Bind(wx.EVT_BUTTON,self.Batman,self.buttons[1])
self.Bind(wx.EVT_BUTTON,self.Rock,self.buttons[2])
self.SetSizerAndFit(self.mainsizer)
self.Show()
def iprint(self,string): #iprint because "internal print", not because Apple
self.textout.SetInsertionPointEnd()
print(string)
def BobDole(self,e):
self.iprint('Bob Dole!')
def Batman(self,e):
self.iprint('The hero we deserve.')
def Rock(self,e):
self.iprint('It looks happy...')
def main():
app=wx.App()
app.locale=wx.Locale(wx.LANGUAGE_ENGLISH)
somegui=SomeGUI(None, title='It prints text and stuff')
app.MainLoop()
if __name__=='__main__':
main()
从技术上讲,我认为运行时间会比禁用wx.TextCtrl慢一点,但实际上看不到这是一个问题。