Python 2.7 wxPython按钮和弹出消息

Python 2.7 wxPython按钮和弹出消息,python-2.7,button,dialog,wxpython,panel,Python 2.7,Button,Dialog,Wxpython,Panel,我正在创建一个wxPython应用程序,其中列出了一行行按钮。按下按钮时,将显示一条弹出消息(将是报价)。我无法对按钮进行编程以显示弹出消息 1) 单击wx.ToggleButton后显示弹出消息时遇到问题 2) 另一个问题是如何制作多个按钮,每个按钮将显示不同的消息 import wx class MyDialog(wx.Dialog): def __init__(self, parent, id, title): wx.Dialog.__init__(self,

我正在创建一个wxPython应用程序,其中列出了一行行按钮。按下按钮时,将显示一条弹出消息(将是报价)。我无法对按钮进行编程以显示弹出消息

1) 单击wx.ToggleButton后显示弹出消息时遇到问题

2) 另一个问题是如何制作多个按钮,每个按钮将显示不同的消息

 import wx

class MyDialog(wx.Dialog):

    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, size=(350,300))
class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(550,500))

    self.CreateStatusBar() #Creates the Statusbar in bottom
        filemenu = wx.Menu() 
        #About and Exit
        menuAbout = filemenu.Append(wx.ID_ABOUT, "&About",
" Information about this programme")
        menuExit = filemenu.Append(wx.ID_EXIT, "E&xit",
" Terminate the programme")

        menuBar = wx.MenuBar()
        menuBar.Append(filemenu, "&File")
        self.SetMenuBar(menuBar)

        panel = wx.Panel(self, -1)
        wx.ToggleButton(panel, 1, 'Quote1', (100,100))

        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)






    def Quote1(self, e):

        description = """Message Here"""



    def OnAbout(self, e):
        dlg = wx.MessageDialog( self, "About here ")

        dlg.ShowModal()
        dlg.Destroy()

    def OnExit(self, e):
        self.Close(True)


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

app = MyApp(0)
app.MainLoop()

创建一系列按钮并将它们绑定到同一个处理程序相当容易。几年前我就写过这个话题。使用该示例,我使用您的示例创建了一些简单的内容:

import wx

class TransientMessage(wx.PopupTransientWindow):

    def __init__(self, parent, style, message):
        wx.PopupTransientWindow.__init__(self, parent, style)

        text = wx.StaticText(self, label=message)
        sz = text.GetBestSize()
        self.SetSize( (sz.width+20, sz.height+20))


class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(550,500))

        self.CreateStatusBar() #Creates the Statusbar in bottom
        filemenu = wx.Menu()
        #About and Exit
        menuAbout = filemenu.Append(wx.ID_ABOUT, "&About",
                                    " Information about this programme")
        menuExit = filemenu.Append(wx.ID_EXIT, "E&xit",
                                   " Terminate the programme")

        self.quotes = {'btn1': 'quote 1',
                       'btn2': 'another quote',
                       'btn3': 'Fore Score and 7 Years Ago'}

        menuBar = wx.MenuBar()
        menuBar.Append(filemenu, "&File")
        self.SetMenuBar(menuBar)

        panel = wx.Panel(self, -1)
        topSizer = wx.BoxSizer(wx.HORIZONTAL)
        for btn in self.quotes:
            new_btn = wx.Button(panel, label=btn, name=btn)
            topSizer.Add(new_btn, 0, wx.ALL, 5)
            new_btn.Bind(wx.EVT_BUTTON, self.Quote1)

        panel.SetSizer(topSizer)

        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)


    def Quote1(self, e):
        btn = e.GetEventObject()
        quote = self.quotes[btn.GetName()]
        win = TransientMessage(self,
                               wx.SIMPLE_BORDER,
                               quote)

        pos = btn.ClientToScreen( (0,0) )
        sz =  btn.GetSize()
        win.Position(pos, (0, sz[1]))

        win.Popup()


    def OnAbout(self, e):
        dlg = wx.MessageDialog( self, "About here ")

        dlg.ShowModal()
        dlg.Destroy()

    def OnExit(self, e):
        self.Close(True)


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

app = MyApp(0)
app.MainLoop()
您可能需要稍微修改它,以便quote dictionary的值是另一个包含按钮标签和quote的数据结构,而不是使用相同的字符串作为按钮标签和名称