开发者

wxPython: How to reference the frame's status bar from individual notebook panels?

I'm sure this is easy for you guys, but I'm having a hard time finding a solution to this...

I have a Frame that is constructed of a bunch of imported files to build a notebook... something like:

class myFrame(wx.Frame):
    """
    Frame that holds all other widgets
    """

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, wx.ID_ANY, "My Frame", size=(520,635))
        panel = wx.Panel(self)

        notebook = myFrameNotebook(panel)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(notebo开发者_运维技巧ok, 1, wx.ALL|wx.EXPAND, 5)
        panel.SetSizer(sizer)

        self.CreateStatusBar(style=0)
        self.SetStatusText("Welcome to My Frame...")

        self.Layout()

        self.Show()
        self.Centre()

I would like, from within the individual pages of the notebook to change the Status Text of the entire frame... If the above is called myFile, is there a way to SetStatusText from within a separate notebook page (which is stored in a separate file?)

What I'd like to do, for instance, is when you move from notebook page to notebook page, the status bar reflects where inside the application you currently are...?

Thanks much,

Chow (still new to wxPython)


It's best to use PubSub for this, to decouple your class' dependencies on another (and to reduce that GetParent().GetParent() noise :) )

import random, string
import wx

from wx.lib.pubsub import Publisher

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, size=(300, 300))
        self.CreateStatusBar(style=0)
        self.notebook = wx.Notebook(self)
        btn = wx.Button(self, label="add page")
        btn.Bind(wx.EVT_BUTTON, lambda evt: self.create_page(random.choice(string.letters)))

        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(btn, 0)
        box.Add(self.notebook, 1, wx.EXPAND)
        self.SetSizer(box)

        Publisher().subscribe(self.change_statusbar, 'change_statusbar')
        self.create_page("1")
        self.create_page("2")
        self.Layout()

    def create_page(self, text):
        self.notebook.AddPage(Page(self.notebook, text), text)

    def change_statusbar(self, msg):
        self.SetStatusText(msg.data)


class Page(wx.Panel):
    def __init__(self, parent, text):
        wx.Panel.__init__(self, parent)
        Publisher().sendMessage(('change_statusbar'), text)


app = wx.App(redirect=False)
top = Frame()
top.Show()
app.MainLoop()


Try something like this (not very familiar with Python but this is what I would do in C++). In your constructor:

self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged)

Write a new method in your Frame class:

def OnPageChanged(self, event):
    selected_page = event.GetSelection()
    if selected_page == wx.NOT_FOUND:
        self.SetStatusText("No page selected!")
    else:
        self.SetStatusText("Welcome to notebook page: " + event.GetEventObject().GetPageText(selected_page)

I don't have wxPython installed so this is untested, but it's the general idea. This will set the status bar to reflect the title of whatever page is selected. If you need it to show some other data that is stored in the page, you could probably take advantage of Python's being dynamic and just add a property to each page:

def OnLoadPages(self):
    # Read pages from a file
    # Every time you add a page, do this:
    page.status_bar_info = whatever_you_want_to_put_here
def OnPageChanged(self, event):
    # Basically the same as above
    # Instead of calling GetPageText, do this:
    self.SetStatusText(event.GetEventObject().GetCurrentPage().status_bar_info)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜