面板内部的wxPython追加和弹出消息框,就像一个通知中心

时间:2018-10-16 06:21:23

标签: python wxpython wxpython-phoenix

我想制作一个具有通知中心的wxpython程序,就像Windows或Mac上的通知中心一样。每当我收到一条消息时,该消息就会显示在通知面板中,然后用户可以关闭该消息。

我有一个示例代码,如下所示:

import wx
import wx.lib.scrolledpanel as scrolled

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)

        topPanel = wx.Panel(self)

        panel1 = wx.Panel(topPanel, -1)
        button1 = wx.Button(panel1, -1, label="generate message")

        self.panel2 = scrolled.ScrolledPanel(
            topPanel, -1, style=wx.SIMPLE_BORDER)
        self.panel2.SetAutoLayout(1)
        self.panel2.SetupScrolling()

        button1.Bind(wx.EVT_BUTTON, self.onAdd)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(panel1,-1,wx.EXPAND|wx.ALL,border=10)
        sizer.Add(self.panel2,-1,wx.EXPAND|wx.ALL,border=10)

        self.sizer2 = wx.BoxSizer(wx.VERTICAL)

        topPanel.SetSizer(sizer)
        self.panel2.SetSizer(self.sizer2)

    def onAdd(self, event):
        new_text = wx.TextCtrl(self.panel2, value="New Message")
        self.sizer2.Add(new_text,0,wx.EXPAND|wx.ALL,border=1)
        self.panel2.Layout()
        self.panel2.SetupScrolling()


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

app = MyApp(0)
app.MainLoop()

在上面的I代码中,右侧面板(即panel2)用作通知中心,所有消息均应显示在其中。在左侧面板(即panel1)上,我有一个按钮来生成消息,仅用于模仿通知行为。理想情况下,右侧面板上的消息应该是可以关闭的消息框(可能是框架?还是MessageDialog?)

任何提示或建议都值得赞赏,最好举个例子!

谢谢!

1 个答案:

答案 0 :(得分:1)

终于弄清楚了自己,这比我最初想象的要容易。

代码如下:

import wx
import wx.lib.scrolledpanel as scrolled

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)

        self.number_of_panels = 0

        topPanel = wx.Panel(self)

        panel1 = wx.Panel(topPanel, -1)
        button1 = wx.Button(panel1, -1, label="generate message")

        self.panel2 = scrolled.ScrolledPanel(
            topPanel, -1, style=wx.SIMPLE_BORDER)
        self.panel2.SetAutoLayout(1)
        self.panel2.SetupScrolling()

        button1.Bind(wx.EVT_BUTTON, self.onAdd)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(panel1,0,wx.EXPAND|wx.ALL,border=5)
        sizer.Add(self.panel2,1,wx.EXPAND|wx.ALL,border=5)

        self.sizer2 = wx.BoxSizer(wx.VERTICAL)

        topPanel.SetSizer(sizer)
        self.panel2.SetSizer(self.sizer2)

    def onAdd(self, event):
        self.number_of_panels += 1
        panel_label = "Panel %s" %  self.number_of_panels
        panel_name = "panel%s" % self.number_of_panels

        new_panel = wx.Panel(self.panel2, name=panel_name, style=wx.SIMPLE_BORDER)

        self.closeButton = wx.Button(new_panel, label='Close %s' % self.number_of_panels)
        self.closeButton.panel_number = self.number_of_panels

        self.closeButton.Bind(wx.EVT_BUTTON, self.OnClose)

        self.sizer2.Add(new_panel,0,wx.EXPAND|wx.ALL,border=1)
        self.panel2.Layout()
        self.panel2.SetupScrolling()

    def OnClose(self, e):

        if self.panel2.GetChildren():
            e.GetEventObject().GetParent().Destroy()
            self.number_of_panels -= 1
            self.panel2.Layout()  # Reset layout after destroy the panel


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

app = MyApp(0)
app.MainLoop()

基本上,我可以破坏新创建的面板。单击关闭按钮时,我只需要知道它是哪个面板即可。这应该与通知中心非常相似。