【问题标题】:How to use wxPython to create a dialogue box?如何使用wxPython创建对话框?
【发布时间】:2013-12-18 11:38:16
【问题描述】:

我正在学习使用 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 IDLEApatana 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: No module named run

我可以知道我错了吗?非常感谢。 :)

【问题讨论】:

    标签: python wxpython


    【解决方案1】:

    ImportError 是 Python 解释器(运行 Python 代码的程序),让您知道它找不到您尝试导入的模块(.py 文件)。具体来说,错误是说它找不到您在第 61 行要求它导入的模块“运行”。

    当您在 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 中的错误发生了什么。这很奇怪!

    【讨论】:

    • 嗨,本。非常感谢非常详细的回答。这真的很有帮助。如果我可以再问一个问题:TestPanel 类的构造函数中的log 是什么?当我运行代码时,在我选择一个目录后,会弹出以下错误:Traceback (most recent call last): File "P:/Project Backups/Python Practice/Examples/Demo_Panel2.py", line 25, in OnButton self.log.WriteText('You selected:' % dlg.GetPath()) AttributeError: 'int' object has no attribute 'WriteText' 。我认为这个错误指向log。非常感谢。
    • 这段代码是从 wxpython 演示代码中复制的 DirDialog 日志用于在演示中显示输出。您可以注释掉任何对日志的引用。
    • 优秀的侦探! run.py 模块还包含一个 Log 类,用于显示您所做的选择。正如 Yoriz 所说,您可以删除对日志的引用。我选择将日志类添加到上面的示例代码中,这样您也可以看到它是如何适应的。它是从 run.py 复制的,但我将类更改为“新样式”类而不是旧类(请参阅stackoverflow.com/questions/54867/…)并将 TestPanel 中的一行从 self.log = log 更改为 self.log = Log()
    猜你喜欢
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2011-01-31
    • 2015-02-03
    • 2011-04-19
    相关资源
    最近更新 更多