Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wxPython附加并弹出面板内的消息框,类似于通知中心_Python_Wxpython - Fatal编程技术网

wxPython附加并弹出面板内的消息框,类似于通知中心

wxPython附加并弹出面板内的消息框,类似于通知中心,python,wxpython,Python,Wxpython,我想做一个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,

我想做一个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)上,我有一个按钮来生成消息,以模拟通知行为。理想情况下,右侧面板上的消息应该是一个可以关闭的消息框(可能是一个框架?或消息对话框?)

任何提示或建议都将不胜感激,最好是举个例子


谢谢

我终于明白了,这比我最初想象的要容易

代码如下:

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()
基本上我可以摧毁新创建的面板。我只需要在单击“关闭”按钮时知道它是哪个面板。这应该与通知中心非常相似