【问题标题】:wxPython auinotebook.GetSelection() return index to the first pagewxPython auinotebook.GetSelection() 返回第一页的索引
【发布时间】:2009-03-15 14:37:49
【问题描述】:

为什么 'GetSelection()' 将索引返回到第一页,而不是在 'init' 和 'new_panel' 中创建的最后一页?它确实在“点击”方法中返回正确的索引。

输出应该是 0 0 1 1 2 2,但我的是 0 0 0 0 0 0。

在 ArchLinux 中运行最新版本的 python 和 wxpython。

奥扬·彼得森

#!/usr/bin/python

#12_aui_notebook1.py

import wx
import wx.lib.inspection

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)

        self.nb = wx.aui.AuiNotebook(self)

        self.new_panel('Page 1')
        print self.nb.GetSelection()
        self.new_panel('Page 2')
        print self.nb.GetSelection()
        self.new_panel('Page 3')
        print self.nb.GetSelection()

    def new_panel(self, nm):
        pnl = wx.Panel(self)
        pnl.identifierTag = nm
        self.nb.AddPage(pnl, nm)
        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.nb, 1, wx.EXPAND)
        self.SetSizer(self.sizer)
        pnl.SetFocus() # Have focused the last panel.
        print self.nb.GetSelection()

        pnl.Bind(wx.EVT_LEFT_DOWN, self.click)

    def click(self, event):
        print 'Mouse click'
        print self.nb.GetSelection()
        print self.nb.GetPageText(self.nb.GetSelection())

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, '12_aui_notebook1.py')
        frame.Show()
        self.SetTopWindow(frame)
        return 1

if __name__ == "__main__":
    app = MyApp(0)
#    wx.lib.inspection.InspectionTool().Show()
    app.MainLoop()

【问题讨论】:

    标签: python wxpython wxwidgets


    【解决方案1】:

    解决方案非常简单。问题似乎是创建新页面并没有生成页面更改事件。 解决办法是:

    self.nb.AddPage(pnl, nm, select=True)
    

    添加 'select=True' 将触发页面更改事件。这样问题就解决了。

    另一种解决方案是添加这一行:

    self.nb.SetSelection(self.nb.GetPageCount()-1)
    

    他们都做同样的事情。触发页面更改事件到最后添加的页面。

    def new_panel(self, nm):
            pnl = wx.Panel(self)
            pnl.identifierTag = nm
            self.nb.AddPage(pnl, nm, select=True) 
            self.sizer = wx.BoxSizer()
            self.sizer.Add(self.nb, 1, wx.EXPAND)
            self.SetSizer(self.sizer)
            #self.nb.SetSelection(self.nb.GetPageCount()-1)
            pnl.SetFocus() # Have focused the last panel.
            print self.nb.GetSelection()
    

    【讨论】:

      【解决方案2】:

      我运行了您的示例并得到了正确的输出:

      0
      0
      1
      1
      2
      2
      

      我正在使用 wxPython 的最新 Windows 版本

      【讨论】:

      • 这很奇怪,我得到 0 0 0 0 0 0。在 linux 平台上运行它。那么我可能没有做错什么?
      猜你喜欢
      • 2019-08-13
      • 1970-01-01
      • 2023-01-17
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多