2012-09-12 19 views
0

我正在使用wxpython製作這個GUI工具。爲此我有一個菜單和一些菜單項。現在,當我點擊一個特定的菜單項時,我已經編寫了處理菜單項點擊事件的代碼。它會創建一個新工作表(面板和其中的一個listctrl),並將該頁面添加到已創建的wx.Notebook對象中。現在,當我點擊一個接一個的菜單項時,我希望連續打開的選項卡處於活動狀態(也就是那個時刻向用戶顯示的選項卡),而實際發生的情況是打開的第一個選項卡是一個保持活躍的人。請問我可以怎樣做到這一點?wxpython:如何通過事件處理程序打開選項卡以激活選項卡?

這裏是事件處理程序的代碼:

# code for one menu-item click - 
def displayApps(self, event): 
    self.appsTab = TabPanel(self.notebook) 
    self.notebook.AddPage(self.appsTab, "List of applications running on each node") 
    self.apps = wx.ListBox(self.appsTab, 12, (10, 40),(450,150), self.appslist, wx.LB_SINGLE) #creating the listbox inside the panel in the tab 

# code for another menu-item click - 
def displayFreeNodes(self, event): 
    #displays the list of free nodes in panel1 
    self.freenodesTab = TabPanel(self.notebook) 
    self.notebook.AddPage(self.freenodesTab, "List of free nodes in the cluster") 
    self.freenodes = wx.ListBox(self.freenodesTab, 13, (10,40),(200,130), self.freenodeslist, wx.LB_SINGLE) 
    #self.boxsizer1.Add(self.freenodes, 1) 

回答

1

邁克·德里斯科爾,誰是真正的相當活躍在這裏SO,寫a blog post將告訴您如何更改一個wx.Notebook頁面。它看起來像你想要使用wx.Notebook.SetSelection()方法。不幸的是,這個方法的文檔並沒有使這個功能清晰。

SetSelection()將索引作爲參數,因此您需要計算正確的索引。假設每個新頁面被追加到wx.Notebook的末尾,您應該可以使用wx.Notebook.GetPageCount()函數來計算總頁數,從而計算最終頁面的索引。您的代碼應該是這樣的:

def displayFreeNodes(self, event): 

    [...] 

    index = self.notebook.GetPageCount() - 1 #get the index of the final page. 
    self.notebook.SetSelection(index) #set the selection to the final page 

編輯
看來我稍微誤解了這個問題。 OP希望能夠根據用戶單擊wx.ListCtrl對象中的相應項目來選擇打開哪個選項卡。

最簡單的方法是確保這些項目總是以中出現的順序出現在wx.ListCtrl中。這意味着列表中的單擊索引0在筆記本中打開索引0,單擊1打開標籤1,依此類推。在這種情況下,你需要捕捉wx.EVT_LIST_ITEM_SELECTED並將其綁定到類似於下面的方法:

def handleListItemClick(event): 
    index = event.GetIndex() #this will return the index of the listctrl item that was clicked 
    self.notebook.SetSelection(index) #this will open that same index in notebook 
+0

感謝您的客氣話並鏈接到我的博客。我很高興人們仍然覺得我的舊文章很有用。 –

+0

謝謝你,完美的工作! – gravetii

+0

關於同一個腳本的另外一個問題 - 如何避免打開已經在筆記本中打開的選項卡(當用戶點擊之前的同一個菜單項以及何時調用事件處理程序時)。如同當​​用戶再次點擊相同的菜單項時,已經打開的標籤變爲活動標籤,並且具有相同信息的新標籤不再打開。我怎樣才能做到這一點? – gravetii

0

我會用爲setSelection或ChangeSelection。這裏有一個有趣的小劇本我颳起了一陣,顯示如何做到這一點(注:你必須嘗試使用​​「下一頁」菜單項前增加一對夫婦頁):

import random 
import wx 

######################################################################## 
class NewPanel(wx.Panel): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self, parent): 
     """Constructor""" 
     wx.Panel.__init__(self, parent) 

     color = random.choice(["red", "blue", "green", "yellow"]) 
     self.SetBackgroundColour(color) 

######################################################################## 
class MyNotebook(wx.Notebook): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self, parent): 
     """Constructor""" 
     wx.Notebook.__init__(self, parent) 

######################################################################## 
class MyFrame(wx.Frame): 
    """""" 

    #---------------------------------------------------------------------- 
    def __init__(self): 
     """Constructor""" 
     wx.Frame.__init__(self,None, title="NoteBooks!") 
     self.page_num = 1 

     panel = wx.Panel(self) 
     self.notebook = MyNotebook(panel) 

     sizer = wx.BoxSizer(wx.VERTICAL) 
     sizer.Add(self.notebook, 1, wx.EXPAND) 
     panel.SetSizer(sizer) 
     self.createMenu() 

     self.Layout() 
     self.Show() 

    #---------------------------------------------------------------------- 
    def createMenu(self): 
     """""" 
     menuBar = wx.MenuBar() 
     fileMenu = wx.Menu() 

     addPageItem = fileMenu.Append(wx.NewId(), "Add Page", 
             "Adds new page") 
     nextPageItem = fileMenu.Append(wx.NewId(), "Next Page", 
             "Goes to next page") 

     menuBar.Append(fileMenu, "&File") 
     self.Bind(wx.EVT_MENU, self.onAdd, addPageItem) 
     self.Bind(wx.EVT_MENU, self.onNext, nextPageItem) 
     self.SetMenuBar(menuBar) 

    #---------------------------------------------------------------------- 
    def onAdd(self, event): 
     """""" 
     new_page = NewPanel(self.notebook) 
     self.notebook.AddPage(new_page, "Page %s" % self.page_num) 
     self.page_num += 1 

    #---------------------------------------------------------------------- 
    def onNext(self, event): 
     """""" 
     number_of_pages = self.notebook.GetPageCount() 
     page = self.notebook.GetSelection()+1 
     if page < number_of_pages: 
      self.notebook.ChangeSelection(page) 

#---------------------------------------------------------------------- 
if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MyFrame() 
    app.MainLoop() 
+0

謝謝。這工作就像我想要的一樣! – gravetii