wxPython:如何从各个笔记本面板引用框架的状态栏?

发布于 2024-11-27 06:16:06 字数 952 浏览 4 评论 0原文

我确信这对你们来说很容易,但我很难找到解决方案...

我有一个由一堆导入文件构建的框架来构建笔记本...类似:

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(notebook, 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()

我想从笔记本的各个页面中更改整个框架的状态文本...如果上面的内容称为 myFile,有没有办法从单独的笔记本页面中 SetStatusText (它存储在一个单独的文件中?)

我想要做什么,对于例如,当您从笔记本页面移动到笔记本页面时,状态栏会反映您当前在应用程序中的位置...?

非常感谢,

Chow(对 wxPython 还是个新手)

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(notebook, 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我三岁 2024-12-04 06:16:06

为此,最好使用 PubSub,以解耦类对另一个类的依赖关系(并减少 GetParent().GetParent() 噪音:))

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()

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()
み青杉依旧 2024-12-04 06:16:06

尝试这样的事情(对Python不太熟悉,但这是我在C++中要做的)。在你的构造函数中:

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

在你的 Frame 类中编写一个新方法:

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)

我没有安装 wxPython,所以这是未经测试的,但这是一般的想法。这将设置状态栏以反映所选页面的标题。如果您需要它来显示页面中存储的一些其他数据,您可能可以利用 Python 的动态性,只需向每个页面添加一个属性:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文