好的代码:
#!/usr/bin/python
import wx
import sys
class XPinst(wx.App):
def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)
def OnInit(self):
frame = wx.Frame(None, -1, title='Redirect Test', size=(620,450), style=wx.STAY_ON_TOP|wx.DEFAULT_FRAME_STYLE)
panel = wx.Panel(frame, -1)
log = wx.TextCtrl(panel, -1, size=(500,400), style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
redir=RedirectText(log)
sys.stdout=redir
print 'test'
frame.Show()
return True
class RedirectText:
def __init__(self,aWxTextCtrl):
self.out=aWxTextCtrl
def write(self,string):
self.out.WriteText(string)
app = XPinst()
app.MainLoop()
添加:
class MyFrame(wx.Frame)
def __init__(self, parent, id, title, size=(620,450), style=wx.STAY_ON_TOP|wx.DEFAULT_FRAME_STYLE):
wx.Frame.__init__(self, parent, id, title, size=(620,450), style=wx.STAY_ON_TOP|wx.DEFAULT_FRAME_STYLE)
替换:
frame = wx.Frame(None, -1, title='Redirect Test', size=(620,450), style=wx.STAY_ON_TOP|wx.DEFAULT_FRAME_STYLE)
和:
frame = MyFrame(None, -1, title='Redirect Test', size=(620,450), style=wx.STAY_ON_TOP|wx.DEFAULT_FRAME_STYLE)
现在,它没有运行...
我希望能够在传递不同参数的代码中多次调用 MyFrame 构造函数
我尝试了很多东西...
用所有参数实例化 MyFrame 用所有参数实例化 myFrame 和所有参数,但默认参数
的构造函数方法签名,所有参数,但默认参数的构造
函数方法签名,
调用所有参数的
父构造函数方法,调用所有参数,但默认参数的父构造函数方法
加上教程http://zetcode.com/wxpython/提到了默认和可选参数数量不同的方法!(有什么不同?)
UDPATE:
“它有七个参数。第一个参数没有默认值。其他六个参数有。这四个参数是可选的。前三个是强制性的。” - http://zetcode.com/wxpython/firststeps/
更新 2:
通过分号更正,我刚刚尝试过:
class MyFrame(wx.Frame):
def __init__(self, parent, id, title, size, style):
wx.Frame.__init__(self, parent, id, title, size, style)
- 我告诉你有什么论据(第二行)
- 我用输入的参数打电话(第三行)
更新 3:
完整的错误信息是:
Traceback (most recent call last):
File "test.py", line 29, in <module>
app = XPinst()
File "test.py", line 8, in __init__
wx.App.__init__(self, redirect, filename)
File "/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode/wx/_core.py", line 7978, in __init__
self._BootstrapApp()
File "/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode/wx/_core.py", line 7552, in _BootstrapApp
return _core_.PyApp__BootstrapApp(*args, **kwargs)
File "test.py", line 10, in OnInit
frame = MyFrame(None, -1, title='Redirect Test', size=(620,450), style=wx.STAY_ON_TOP|wx.DEFAULT_FRAME_STYLE)
File "test.py", line 21, in __init__
wx.Frame.__init__(self, parent, id, title, size, style)
File "/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode/wx/_windows.py", line 497, in __init__
_windows_.Frame_swiginit(self,_windows_.new_Frame(*args, **kwargs))
TypeError: Expected a 2-tuple of integers or a wxSize object.
为什么它不起作用?