Tabs wxpython:通过事件处理程序打开选项卡后,如何使其处于活动状态?

Tabs wxpython:通过事件处理程序打开选项卡后,如何使其处于活动状态?,tabs,event-handling,wxpython,Tabs,Event Handling,Wxpython,我使用wxpython制作这个GUI工具。为此,我有一份菜单和一些菜单项。现在,当我单击一个特定的菜单项时,我已经为处理菜单项click的事件编写了代码。它创建一个新的工作表(面板和其中的listctrl),并将页面添加到已创建的wx.Notebook对象中。现在,当我一个接一个地单击菜单项时,我希望依次打开的选项卡处于活动状态(即,此时显示给用户的选项卡),而实际发生的情况是,第一个打开的选项卡保持活动状态。请问如何才能做到这一点 以下是事件处理程序的代码: # code for one me

我使用wxpython制作这个GUI工具。为此,我有一份菜单和一些菜单项。现在,当我单击一个特定的菜单项时,我已经为处理菜单项click的事件编写了代码。它创建一个新的工作表(面板和其中的listctrl),并将页面添加到已创建的
wx.Notebook
对象中。现在,当我一个接一个地单击菜单项时,我希望依次打开的选项卡处于活动状态(即,此时显示给用户的选项卡),而实际发生的情况是,第一个打开的选项卡保持活动状态。请问如何才能做到这一点

以下是事件处理程序的代码:

# 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)

迈克·德里斯科尔(Mike Driscoll)在这里非常活跃,他写了一篇文章,向您展示了如何在
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.Notebook
中出现的顺序相同的顺序出现在
wx.ListCtrl
中。这意味着单击列表的索引0将打开笔记本中的索引0,单击1将打开选项卡1,依此类推。在这种情况下,您需要捕获所选的
wx.EVT\u LIST\u项
,并将其绑定到类似以下的方法:

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

我会使用SetSelection或ChangeSelection。下面是一个有趣的小脚本,演示了如何操作(注意:在尝试使用“下一页”菜单项之前,您必须添加几页):

随机导入
导入wx
########################################################################
新面板类(wx.Panel):
""""""
#----------------------------------------------------------------------
定义初始化(自身,父级):
“构造函数”
wx.Panel.\uuuuu init\uuuuuuuuuuuux(自,父)
颜色=随机。选择([“红”、“蓝”、“绿”、“黄”])
self.setbackgroundColor(颜色)
########################################################################
类别MyNotebook(wx.Notebook):
""""""
#----------------------------------------------------------------------
定义初始化(自身,父级):
“构造函数”
wx.Notebook.\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
########################################################################
类MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
定义初始化(自):
“构造函数”
wx.Frame.\uuuu init\uuuuu(self,None,title=“笔记本!”)
self.page_num=1
面板=wx.面板(自)
self.notebook=MyNotebook(面板)
sizer=wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.notebook,1,wx.EXPAND)
面板设置器(施胶器)
self.createMenu()
self.Layout()
self.Show()
#----------------------------------------------------------------------
def创建菜单(自身):
""""""
menuBar=wx.menuBar()
fileMenu=wx.Menu()
addPageItem=fileMenu.Append(wx.NewId(),“添加页面”,
“添加新页面”)
nextPageItem=fileMenu.Append(wx.NewId(),“下一页”,
“转到下一页”)
menuBar.Append(文件菜单“&File”)
self.Bind(wx.EVT_菜单、self.onAdd、addPageItem)
self.Bind(wx.EVT_菜单、self.onNext、nextPageItem)
self.SetMenuBar(menuBar)
#----------------------------------------------------------------------
def onAdd(自身、事件):
""""""
新建页面=新建面板(self.notebook)
self.notebook.AddPage(新建页面,“页面%s”%self.page\u num)
self.page_num+=1
#----------------------------------------------------------------------
def onNext(自身、事件):
""""""
页数=self.notebook.GetPageCount()
page=self.notebook.GetSelection()+1
如果页数<页数:
self.notebook.ChangeSelection(第页)
#----------------------------------------------------------------------
如果名称=“\uuuuu main\uuuuuuuu”:
app=wx.app(假)
frame=MyFrame()
app.MainLoop()

谢谢你的好话和我的博客链接。我很高兴人们仍然觉得我的老文章很有用。关于同一个脚本,还有一个问题-如何避免打开笔记本中已经打开的选项卡(当用户先前单击同一菜单项时,以及当调用事件处理程序时)。与中一样,当用户再次单击同一菜单项时,已打开的选项卡将变为活动选项卡,并且具有相同信息的新选项卡不会再次打开。我怎么能做到呢?哦,我有点误解了你的意图。给我一点时间,我会给我的答案添加一个解释。。。使用SingleInstanceChecker方法获得答案!谢谢
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()