wxpython:如果通过事件处理程序打开选项卡,该选项卡是否处于活动状态? [英] wxpython: How to make a tab active once it is opened via an event handler?
问题描述
wx.Notebook
对象中。现在,当我单击一个菜单项之后,我希望连续打开的选项卡是活动的(即那个时候向用户显示的),而实际发生的是打开的第一个选项卡是一个保持活跃。请问这可以实现吗? 以下是事件处理程序的代码:
#一个菜单项的代码点击 -
def displayApps(self,event):
self.appsTab = TabPanel(self.notebook)
self。 notebook.AddPage(self.appsTab,每个节点上运行的应用程序列表)
self.apps = wx.ListBox(self.appsTab,12,(10,40),(450,150),self.appslist, wx.LB_SINGLE)#创建标签中面板中的列表框
#另一个菜单项的代码点击 -
def displayFreeNodes(self,event):
#displays panel1中的空闲节点列表
self.freenodesTab = TabPanel(self.notebook)
self.notebook.AddPage(self.freenodesTab,集群中的空闲节点列表)
self。 freenodes = wx.ListBox(self.freenodesTab,13,(10,40),(200,130),self.freenodeslist,wx.LB_SINGLE)
#self.boxsizer1.Add(self.freenodes,1)
e Driscoll在SO上实际上非常活跃,写了一个博文,向您展示如何更改 wx.Notebook
中的页面。看起来你想使用 wx.Notebook.SetSelection()
方法。不幸的是,此方法的文档并不能使此功能清楚。
SetSelection()
参数,所以你需要计算正确的索引。假设每个新页面都附加到 wx.Notebook
的末尾,您应该可以使用 wx.Notebook.GetPageCount()
函数来计算总页数,从而得到最终页面的索引。您的代码应如下所示:
def displayFreeNodes(self,event):
[.. 。]
index = self.notebook.GetPageCount() - 1 #get最后一页的索引。
self.notebook.SetSelection(index)#将选择设置为最终页面
EDIT
看来我稍微误解了这个问题。 OP希望能够根据用户点击 wx.ListCtrl
对象中的相应项目来选择要打开哪个标签。
最简单的方法是确保项目始终以 wx.ListCtrl
中的顺序显示在 wx.Notebook
。这意味着单击列表索引0将在笔记本中打开索引0,单击1打开选项卡1,依此类推。在这种情况下,您需要捕获 wx.EVT_LIST_ITEM_SELECTED
并将其绑定到类似于以下的方法:
def handleListItemClick(event):
index = event.GetIndex()#this将返回被点击的listctrl项目的索引
self.notebook.SetSelection(index) #这将在笔记本中打开相同的索引
I am making this GUI tool using wxpython. For that I have a menu and a few menu-items. Now, when I click on a particular menu-item, I have written the code for the event that handles the menu-item click. It creates a new sheet(panel, and a listctrl inside it) and adds the page to the already-created wx.Notebook
object. Now, when I click on one menu-item after another, I want the tabs opened successively to be active (that is, the one that is shown to the user at that moment), whereas what actually happens is that the first tab opened is the one that stays active. May I please know how this can be achieved?
Here is the code for the event-handlers:
# 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, who is actually quite active here on SO, has written a blog post that shows you how to change pages in a wx.Notebook
. It looks like you want to use the wx.Notebook.SetSelection()
method. Unfortunately, the documentation for this method does not make this functionality clear.
SetSelection()
takes an index as it's argument, so you need to calculate the proper index. Assuming each new page is appended to the end of the wx.Notebook
, you should be able to use the wx.Notebook.GetPageCount()
function to calculate the total number of pages, and thus the index of the final page. Your code should look like this:
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
EDIT
It seems I have slightly misunderstood the question. The OP wants to be able to choose which tab to open based on the user clicking the appropriate item in a wx.ListCtrl
object.
The easiest way to do this would be to ensure that the items always appear in the wx.ListCtrl
in the same order of they appear in the wx.Notebook
. That means that clicking index 0 of the list opens index 0 in the notebook, clicking 1 opens tab 1, and so on. In that case you need to catch wx.EVT_LIST_ITEM_SELECTED
and bind it to a method similar to the following:
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
这篇关于wxpython:如果通过事件处理程序打开选项卡,该选项卡是否处于活动状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!