0

我对编程以及 python 和 wxpython 相当陌生。我已经查看了这个代码几个小时,并尝试在网上到处寻找答案。单击菜单项后,我无法显示新窗口。到目前为止,这是我的代码...

import wx

class MainWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Python Test App',size=(600,400))
        panel=wx.Panel(self)
        wx.Frame.CenterOnScreen(self)
        ##wx.Frame.Maximize(self)

        status=self.CreateStatusBar()
        menubar=wx.MenuBar()
        file_menu=wx.Menu()
        edit_menu=wx.Menu()

        ID_FILE_NEW = 1
        ID_FILE_OPEN = 2

        ID_EDIT_UNDO = 3
        ID_EDIT_REDO = 4


        file_menu.Append(ID_FILE_NEW,"New Window","This is a new window")
        file_menu.Append(ID_FILE_OPEN,"Open...","This will open a new window")

        edit_menu.Append(ID_EDIT_UNDO,"Undo","This will undo your last action")
        edit_menu.Append(ID_EDIT_REDO,"Redo","This will redo your last undo")


        menubar.Append(file_menu,"File")
        menubar.Append(edit_menu,"Edit")
        self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU, NewWindow.new_frame, None, 1)

class NewWindow(wx.Frame):

    def __init__(self,MainWindow,id):
        wx.Frame.__init__(self, None, id, 'New Window', size=(600,400))
        wx.Frame.CenterOnScreen(self)
        self.Show(False)

    def new_frame(self, event):
        NewWindow.Show(True)



if __name__=='__main__':
        app=wx.PySimpleApp()
        frame=MainWindow(parent=None,id=-1)
        frame.Show()
        app.MainLoop()

当我尝试运行此代码时,一旦单击菜单项“新窗口”,就会收到此错误消息

TypeError: unbound method new_frame() must be called with NewWindow instance as first argument (got CommandEvent instance instead)

同样,我对编程还很陌生。非常感谢任何帮助,而且,我知道我的代码可能不是“最干净”的代码。提前致谢!

4

2 回答 2

2

您似乎不了解 Python 中的类是如何工作的。您尝试调用NewWindow.new_frame,但实际上从未创建该类的实例。

错误消息是因为您在类而不是类的实例上调用方法。你想要做的是这样的:

newWin = NewWindow(...) # replace ... with the appropriate parameters
newWin.Show(True)

您在示例中没有提供足够的信息来了解 NewWindow 调用的适当参数是什么(例如,您没有显示创建主窗口的位置),但MainWindowandid参数NewWindow.__init__不仅仅用于外观: wxPython 需要知道父窗口。您应该查看 wxPython 文档以了解如何创建 wxFrame。

于 2012-06-26T04:34:24.557 回答
1

在某种程度上修改您的代码,当用户单击新窗口选项时,我能够显示一个新窗口,

请检查我修改过的东西,让我知道这是否是您想要的?

import wx

class MainWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Python Test App',size=(600,400))
        panel=wx.Panel(self)
        wx.Frame.CenterOnScreen(self)

        status=self.CreateStatusBar()
        menubar=wx.MenuBar()
        file_menu=wx.Menu()
        edit_menu=wx.Menu()

        ID_FILE_NEW = 1
        ID_FILE_OPEN = 2

        ID_EDIT_UNDO = 3
        ID_EDIT_REDO = 4


        file_menu.Append(ID_FILE_NEW,"New Window","This is a new window")
        file_menu.Append(ID_FILE_OPEN,"Open...","This will open a new window")

        edit_menu.Append(ID_EDIT_UNDO,"Undo","This will undo your last action")
        edit_menu.Append(ID_EDIT_REDO,"Redo","This will redo your last undo")


        menubar.Append(file_menu,"File")
        menubar.Append(edit_menu,"Edit")
        self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU, self.test, None, 1)

    def test(self, event):
        self.new = NewWindow(parent=None, id=-1)
        self.new.Show()

class NewWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self, parent, id, 'New Window', size=(400,300))
        wx.Frame.CenterOnScreen(self)
        #self.new.Show(False)

if __name__=='__main__':
        app=wx.PySimpleApp()
        frame=MainWindow(parent=None,id=-1)
        frame.Show()
        app.MainLoop()
于 2012-06-26T05:43:10.630 回答