2012-07-12 48 views
0

我是wxpython的新手,一直在試圖在書籍中插入一些窗口部件(treebook,notebook,choicebook)我經常以放置在容器內的一些窗口部件沒有響應事件。我不確定我做錯了什麼。以下是我的代碼之一wxpython窗口部件內沒有對事件作出響應

import wx 

class ChoicePanelTwo(wx.Panel): 
    def __init__(self, parent, seed): 
     wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY) 
     self.SetBackgroundColour('blue') 
     sizer = wx.BoxSizer(wx.HORIZONTAL) 
     self.List = wx.ListCtrl(self, -1, style=wx.LC_REPORT) 
     for i in range(seed): 
      self.List.InsertStringItem(i, str(i)) 
     sizer.Add(self.List, 1, wx.ALL|wx.EXPAND, 5) 
     self.SetSizer(sizer) 

class ChoicePanelOne(wx.Panel): 
    def __init__(self, parent, seed): 
     wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY) 
     self.SetBackgroundColour('green') 
     sizer = wx.BoxSizer(wx.HORIZONTAL) 
     self.RegisterList = wx.Choicebook(self, wx.ID_ANY) 
     sizer.Add(self.RegisterList, 1, wx.ALL|wx.EXPAND, 5) 
     for i in range(seed): 
      self.RegisterList.AddPage(ChoicePanelTwo(self, seed*50), str(i)) 
     self.SetSizer(sizer) 

class TreePanel(wx.Panel): 
    def __init__(self, parent, seed): 
     wx.Panel.__init__(self, parent, id=wx.ID_ANY) 
     self.SetBackgroundColour('cyan') 
     self.Choicbook = wx.Choicebook(self, wx.ID_ANY) 
     for i in range(seed): 
      self.Choicbook.AddPage(ChoicePanelOne(self, seed*2), str(i)) 
     sizer = wx.BoxSizer(wx.VERTICAL) 
     sizer.Add(self.Choicbook, 1, wx.ALL|wx.EXPAND, 5) 
     self.SetSizer(sizer) 

class AppFrame(wx.Frame): 
    """ The main frame of the application 
    """ 
    title = 'Application' 
    WindowSize = (1024, 768) 
    seed = 2 

    def __init__(self): 
     wx.Frame.__init__(self, None, -1, self.title, size=self.WindowSize, style=wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN) 
     self.create_main_panel() 

    def create_main_panel(self): 
     self.panel = TreePanel(self, self.seed)   

if __name__ == '__main__': 
    app = wx.PySimpleApp() 
    app.frame = AppFrame() 
    app.frame.Show() 
    app.MainLoop() 

在此示例中。選擇書和列表似乎工作。我做錯了什麼?

+0

這是你的全部代碼嗎?你永遠不會調用'Bind()'函數,也不會指定任何處理程序。你想要捕捉什麼事件?當他們抓住他們時,你想讓你的程序做什麼? 「不工作」是什麼意思?請詳細解釋您的問題,包括您希望完成什麼,您當前的行爲是什麼以及您嘗試過的。 – acattle 2012-07-12 10:38:25

+0

嘿,我想說的是,當用戶試圖改變最內層ChoiceBook的組合框時,它不會切換。當用戶嘗試滾動列表時,滾動條不會移動。我並不想捕捉任何事件,但想要切換選擇頁面並滾動。這似乎沒有發生。 – lordsathish 2012-07-12 11:20:54

回答

2

這是一個養育子女的問題。你將所有的ChoicePanelOne實例的父母設置爲TreePanel,當它應該是ChoicBook。你在ChoicePanelOne中做同樣的事情,在那裏你創建了一大堆ChoicePanelTwo,它們的父母是ChoicePanelOne,當他們應該是RegisterList時。查看下面稍微更改的代碼:

import wx 

class ChoicePanelTwo(wx.Panel): 
    def __init__(self, parent, seed): 
     wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY) 
     self.SetBackgroundColour('blue') 
     sizer = wx.BoxSizer(wx.HORIZONTAL) 
     self.List = wx.ListCtrl(self, -1, style=wx.LC_REPORT) 
     for i in range(seed): 
      self.List.InsertStringItem(i, str(i)) 
     sizer.Add(self.List, 1, wx.ALL|wx.EXPAND, 5) 
     self.SetSizer(sizer) 

class ChoicePanelOne(wx.Panel): 
    def __init__(self, parent, seed): 
     wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY) 
     self.SetBackgroundColour('green') 
     sizer = wx.BoxSizer(wx.HORIZONTAL) 
     self.RegisterList = wx.Choicebook(self, wx.ID_ANY) 
     sizer.Add(self.RegisterList, 1, wx.ALL|wx.EXPAND, 5) 
     for i in range(seed): 
      self.RegisterList.AddPage(ChoicePanelTwo(self.RegisterList, seed*50), str(i)) 
     self.SetSizer(sizer) 

class TreePanel(wx.Panel): 
    def __init__(self, parent, seed): 
     wx.Panel.__init__(self, parent, id=wx.ID_ANY) 
     self.SetBackgroundColour('cyan') 
     self.Choicbook = wx.Choicebook(self, wx.ID_ANY) 
     for i in range(seed): 
      self.Choicbook.AddPage(ChoicePanelOne(self.Choicbook, seed*2), str(i)) 
     sizer = wx.BoxSizer(wx.VERTICAL) 
     sizer.Add(self.Choicbook, 1, wx.ALL|wx.EXPAND, 5) 
     self.SetSizer(sizer) 

class AppFrame(wx.Frame): 
    """ The main frame of the application 
    """ 
    title = 'Application' 
    WindowSize = (1024, 768) 
    seed = 2 

    def __init__(self): 
     wx.Frame.__init__(self, None, -1, self.title, size=self.WindowSize, 
          style=wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN) 
     self.create_main_panel() 

    def create_main_panel(self): 
     self.panel = TreePanel(self, self.seed)   

if __name__ == '__main__': 
    app = wx.PySimpleApp() 
    app.frame = AppFrame() 
    app.frame.Show() 
    app.MainLoop() 

您應該下載wxPython演示程序,因爲它有很多很好的示例。另請參閱wiki或我的blog article

您可以使用Widget Inspection Tool來幫助您診斷問題,以及它會告訴您哪些父母是在哪裏以及如何佈置sizer以及其他內容。

+0

嘿,這很有幫助。我會看看這個例子,wiki和博客 – lordsathish 2012-07-12 15:27:15