我正在使用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)
答案 0 :(得分:1)
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
答案 1 :(得分:0)
我会使用SetSelection或ChangeSelection。这是一个有趣的小脚本我掀起来说明了如何做(注意:在尝试使用&#34;下一页&#34;菜单项之前,你必须添加几页):< / p>
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()