wxPython的问题

stYy2002 2005-01-06 03:13:51
问题1:
今天下载了wxPython包,并拷贝了附带的DEMO中例子进行运行。
结果显示main中import run,没有找到该包。是什么原因?
run是什么包?

问题2:
自己写了一段程序,确显示
Traceback (most recent call last):
File "wxclickme.py", line 3, in ?
class ButtonFrame(wxFrame):
File "wxclickme.py", line 6, in ButtonFrame
button = wxButton(self, 111, 'click me!')
NameError: name 'self' is not defined
是什么原因,如何解决?


问题1代码:
import wx
import sys


#---------------------------------------------------------------------------

class MyFrame(wx.Frame):
def __init__(
self, parent, ID, title, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE
):

wx.Frame.__init__(self, parent, ID, title, pos, size, style)
panel = wx.Panel(self, -1)

button = wx.Button(panel, 1003, "Close Me")
button.SetPosition((15, 15))
self.Bind(wx.EVT_BUTTON, self.OnCloseMe, button)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)


def OnCloseMe(self, event):
self.Close(True)

def OnCloseWindow(self, event):
self.Destroy()

#---------------------------------------------------------------------------

class TestPanel(wx.Panel):
def __init__(self, parent, log):
self.log = log
wx.Panel.__init__(self, parent, -1)

b = wx.Button(self, -1, "Create and Show a Frame", (50,50))
self.Bind(wx.EVT_BUTTON, self.OnButton, b)


def OnButton(self, evt):
win = MyFrame(self, -1, "This is a wx.Frame", size=(350, 200),
style = wx.DEFAULT_FRAME_STYLE)
win.Show(True)



#---------------------------------------------------------------------------


def runTest(frame, nb, log):
win = TestPanel(nb, log)
return win


#---------------------------------------------------------------------------


overview = """\
A Frame is a window whose size and position can (usually) be changed by
the user. It usually has thick borders and a title bar, and can optionally
contain a menu bar, toolbar and status bar. A frame can contain any window
that is not a Frame or Dialog. It is one of the most fundamental of the
wxWindows components.

A Frame that has a status bar and toolbar created via the
<code>CreateStatusBar</code> / <code>CreateToolBar</code> functions manages
these windows, and adjusts the value returned by <code>GetClientSize</code>
to reflect the remaining size available to application windows.

By itself, a Frame is not too useful, but with the addition of Panels and
other child objects, it encompasses the framework around which most user
interfaces are constructed.

If you plan on using Sizers and auto-layout features, be aware that the Frame
class lacks the ability to handle these features unless it contains a Panel.
The Panel has all the necessary functionality to both control the size of
child components, and also communicate that information in a useful way to
the Frame itself.
"""


if __name__ == '__main__':
import sys,os
import run
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])



问题2代码:

from wxPython.wx import *

class ButtonFrame(wxFrame):
def _init_(self):
wxFrame._init_(self, NULL, -1, 'wxPython', wxDefaultPosition, (200, 100))
button = wxButton(self, 111, 'click me!')
EVT_BUTTON(self, 111, self.onButton)

def onButton(self, event):
dlg = wxMessageDialog(self, 'Ow, quit it.', 'Whine', wxOK)
dlg.ShowModal()
dlg.Destroy()

class App(wxApp):
def OnInit(self):
frame = ButtonFrame()
frame.Show(true)
return true


app = App(0)
app.MainLoop()
...全文
297 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
stYy2002 2005-01-10
  • 打赏
  • 举报
回复
谢谢大家,我已经了解了python的一些关系。
xyzxyz1111 2005-01-07
  • 打赏
  • 举报
回复
Frame.py里有import run这个语句,而如果你是把Frame.py拷贝的其他目录,则python不能找到run这个模块.所以需要把run.py拷贝到Frame.py同目录下.

wx有很多demo,每个demo都需要一些重复的启动代码,所以可以把这些代码萃取出来,就形成了run.py这个module
stYy2002 2005-01-07
  • 打赏
  • 举报
回复
run.py是起什么作用的。
为什么要这样写呢?
limodou 2005-01-07
  • 打赏
  • 举报
回复
界面元素的继承关系就是类的关系。简单的:

class A
class B
class C

A中包含B,B中包含C,如果都是在类生成时创建实例:

class A:
def __init__(self):
...do something
self.b = B(self, arg1, arg2)

class B:
def __init__(self, parent, arg1, arg2):
...do something
self.parent = parent
self.c = C(self, arg1, arg2)

class C:
def __init__(self, parent, arg1, arg2):
...do something
self.parent = parent

这样每个子元素在创建时都保存了父对象。这样对象是构成一种树的关系,象目录一样。相互之间就可以引用了。当然这只是最简单的方式,并不是最方便的方式。其它方式还有。只要是在创建类中可以把父对象传给子对象。然后子对象可以保存就可以。方法不限。
stYy2002 2005-01-07
  • 打赏
  • 举报
回复
请大家到http://www.gloryview.com/yy/robot_gui.rar下载上面的例子。
我需要大家的帮忙。

main.py
Note.py
我如何把note.py的note加入到main分割窗体的左边呢?
请大家帮忙啊。
然后我还是不太清楚python的界面是怎样的继承关系、以及先后关系。
limodou 2005-01-07
  • 打赏
  • 举报
回复
wxPython中的界面生成时可能是相互包含的,比如开始是一个frame,其中有工具条、菜单、状态栏、还有一些子窗口。当这些在frame中的元素创建时,都需要一个parent的参数。你可以在子元素的创建时保存parent对象,这样通过parent就可以访问了。另外还可以在子元素的__init__方法中加入别的想传入的参数。还有采用全局对象。还可以在父对象创建子对象后,直接对子对象增加属性。方法太多了。

可以参考 NewEdit 的程序。

在: http://wiki.woodpecker.org.cn/moin.cgi/NewEdit
同时在上面还有几个wxPython相关的项目: 天成的LyricNow http://wiki.woodpecker.org.cn/moin.cgi/_e5_a4_a9_e6_88_90
还有EddyXu在sf.net上的FeedNow, 这是在wiki上的链接:
http://wiki.woodpecker.org.cn/moin.cgi/EddyXu
都可以参考。
stYy2002 2005-01-07
  • 打赏
  • 举报
回复
我现在的界面都是在OnInit(self):中画的。
并且都是在一个类中,其中包括Menu,toolbar,tab等等。这些属性变量都是放在OnInit方法中,
那如果我要其他类中访问这些控件,要如何做?
一般各位设计界面的都是如何做的?
有没有其他方便的画界面IDE呢?
xyzxyz1111 2005-01-06
  • 打赏
  • 举报
回复
搂主应该是把demo目录中的Frame.py文件拷贝出来到一个目录下,试图运行,结果就会出现这个问题,只要再把run.py也拷贝到那个目录下酒可以了
limodou 2005-01-06
  • 打赏
  • 举报
回复
wxPython中的demo可以直接运行啊。除非你自已做了什么了。一般在windows上会生成一个快捷方式的。
xyzxyz1111 2005-01-06
  • 打赏
  • 举报
回复
还有 :__init__不是 _init_
xyzxyz1111 2005-01-06
  • 打赏
  • 举报
回复
第一个问题:要把wx/demo/run.py一同copy
第二个问题:应该把第6,7 行多一个缩进,如此

from wxPython.wx import *

class ButtonFrame(wxFrame):
def _init_(self):
wxFrame._init_(self, NULL, -1, 'wxPython', wxDefaultPosition, (200, 100))
button = wxButton(self, 111, 'click me!')
EVT_BUTTON(self, 111, self.onButton)

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧