wxPython追加并弹出面板内的消息框,用作通知中心

2024-06-16 10:49:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

谢谢!你知道吗


Tags: selfadd消息面板defallframeexpand
1条回答
网友
1楼 · 发布于 2024-06-16 10:49:08

我终于明白了,这比我最初想象的要容易。你知道吗

代码如下:

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

基本上我可以销毁新创建的面板。我只需要知道它是哪个面板,当我点击关闭按钮。这应该与通知中心的工作方式非常相似。你知道吗

相关问题 更多 >