2012-06-26 55 views
0

我對編程以及python和wxpython相當陌生。我已經仔細查看了這段代碼,並試圖在網上找到答案。點擊菜單項後,我無法獲得新窗口。這是到目前爲止我的代碼...在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) 

再次收到此錯誤信息,我我對編程相當陌生。任何幫助非常感謝,而且,我知道我的代碼可能不是「最乾淨」的代碼。提前致謝!

+0

只是一個指針,稱這樣'wx.Frame.CenterOnScreen(個體經營)方法'是不是你通常如何調用方法。因爲你在'wx.Frame'的實例中,你可以訪問一個框架的所有方法,所以你可以調用'self.CenterOnScreen()'。唯一需要調用前者的方法的時間是,如果因爲重寫了方法而不再訪問方法(例如,在'__init__'中調用'wx.Frame .__ init__'時) – GP89

回答

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() 
2

您似乎並不瞭解類如何在Python中工作。您嘗試撥打NewWindow.new_frame,但您永遠不會創建該類的實例。

錯誤消息是因爲您正在調用類的方法而不是類的實例。你想要做的是一樣的東西:

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

你不要在你的例子提供足夠的信息來了解相應的參數對於NewWindow呼叫(例如,你不顯示在您創建的主窗口),但NewWindow.__init__中的MainWindowid參數不僅僅適用於外觀:wxPython需要知道父窗口。您應該查看wxPython文檔以瞭解如何創建wxFrame。

相關問題