我正在学习使用wxPython来构建基于对话的程序。
我尝试了以下代码(只是从wxPython Demo中复制):
import wx
#---------------------------------------------------------------------------
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 DirDialog", (50,50))
self.Bind(wx.EVT_BUTTON, self.OnButton, b)
def OnButton(self, evt):
# In this case we include a "New directory" button.
dlg = wx.DirDialog(self, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE
#| wx.DD_DIR_MUST_EXIST
#| wx.DD_CHANGE_DIR
)
# If the user selects OK, then we process the dialog's data.
# This is done by getting the path data from the dialog - BEFORE
# we destroy it.
if dlg.ShowModal() == wx.ID_OK:
self.log.WriteText('You selected: %s\n' % dlg.GetPath())
# Only destroy a dialog after you're done with it.
dlg.Destroy()
#---------------------------------------------------------------------------
def runTest(frame, nb, log):
win = TestPanel(nb, log)
return win
#---------------------------------------------------------------------------
overview = """\
This class represents the directory chooser dialog. It is used when all you
need from the user is the name of a directory. Data is retrieved via utility
methods; see the <code>DirDialog</code> documentation for specifics.
"""
if __name__ == '__main__':
import sys,os
import run
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
我在Python IDLE
和Apatana Studio 3
中运行了上述代码。这就是我得到的。
在Python IDLE
中,我得到了:
IDLE子进程:没有在sys.argv中传递的IP端口。
在Apatana Studio 3
,我得到了:
追踪(最近的呼叫最后):
的模块
文件“C:\ Users \ User \ My Documents \ Aptana Studio 3 Workspace \ Test Dialogue \ main.py”,第61行, import run ImportError:没有名为run
我可以知道我错了吗?非常感谢。 :)
答案 0 :(得分:1)
ImportError是Python解释器(运行Python代码的程序),让您知道它无法找到您尝试导入的模块(.py文件)。具体来说,错误是它找不到你要求它在第61行导入的模块“run”。
当您在Python中导入时,解释器会搜索模块的一堆位置。其中一个是当前目录,其余的是标准位置,例如安装Python库的位置。此页面包含一些信息:http://docs.python.org/2/tutorial/modules.html#the-module-search-path。如果从命令行运行程序,实际上会得到相同的ImportError。这是一个Python错误,而不是Apatana Studio 3错误。
因此,如果将“run.py”复制到包含Python文件的目录中,Python解释器将能够在您要求导入时轻松找到它。另一种方法是将run.py模块保留在原来的位置并在运行时更改sys.path,或者将模块位置添加到PYTHONPATH变量中(有关详细信息,请参阅上面的链接)。
虽然您尝试实现的目标不需要run.py模块。以下是未导入run.py模块的代码示例。我会警告我自己是wxPython的新手,所以可能有更好的方法来做到这一点; - )
import wx
# This Log class is copied from the run module
class Log(object):
def WriteText(self, text):
if text[-1:] == '\n':
text = text[:-1]
wx.LogMessage(text)
write = WriteText
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 DirDialog", (50,50))
self.Bind(wx.EVT_BUTTON, self.OnButton, b)
def OnButton(self, evt):
# In this case we include a "New directory" button.
dlg = wx.DirDialog(self, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE
#| wx.DD_DIR_MUST_EXIST
#| wx.DD_CHANGE_DIR
)
# If the user selects OK, then we process the dialog's data.
# This is done by getting the path data from the dialog - BEFORE
# we destroy it.
if dlg.ShowModal() == wx.ID_OK:
self.log.WriteText('You selected: %s\n' % dlg.GetPath())
# Only destroy a dialog after you're done with it.
dlg.Destroy()
class Frame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__(self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size(300, 150))
panel = TestPanel(self, -1)
class App(wx.App):
def OnInit(self):
self.frame = Frame(parent=None)
self.frame.Show()
self.SetTopWindow(self.frame)
return True
if __name__ == '__main__':
app = App()
app.MainLoop()
我不确定IDLE中的错误是怎么回事。那太奇怪了!