【发布时间】:2010-08-20 00:56:09
【问题描述】:
好的代码:
#!/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.
为什么没用?
【问题讨论】:
-
当你说“它不运行”时,它是做什么的?错误?向我们展示详细信息。
-
屏幕上没有出现窗口,但什么也没有出现
-
您是否将文件命名为
*.pyw?当我这样做时,我也对类语法错误“一无所获”(请参阅我的答案)。将它们命名为*.py和/或从控制台运行它们以进行开发,然后它实际上会向您显示错误。 -
尼克,我得到一个“TypeError: Expected a 2-tuple of integers or a wxSize object.”。
标签: python wxpython optional-parameters keyword-argument