0

我正在使用 wxpython 制作这个 GUI 工具。为此,我有一个菜单和一些菜单项。现在,当我单击一个特定的菜单项时,我已经为处理菜单项单击的事件编写了代码。它创建一个新工作表(面板和其中的 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)
4

2 回答 2

1

Mike Driscoll 实际上在 SO 上非常活跃,他写了一篇博文,向您展示如何更改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以它们出现在 中的相同顺序出现wx.Notebook。这意味着单击列表的索引 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
于 2012-09-12T13:54:47.563 回答
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()
于 2012-09-12T14:16:45.667 回答