我對編程以及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)
再次收到此錯誤信息,我我對編程相當陌生。任何幫助非常感謝,而且,我知道我的代碼可能不是「最乾淨」的代碼。提前致謝!
只是一個指針,稱這樣'wx.Frame.CenterOnScreen(個體經營)方法'是不是你通常如何調用方法。因爲你在'wx.Frame'的實例中,你可以訪問一個框架的所有方法,所以你可以調用'self.CenterOnScreen()'。唯一需要調用前者的方法的時間是,如果因爲重寫了方法而不再訪問方法(例如,在'__init__'中調用'wx.Frame .__ init__'時) – GP89