wxPython按钮和弹出消息

时间:2015-07-14 01:47:09

标签: 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, 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()

1 个答案:

答案 0 :(得分:0)

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

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

您可能希望稍微更改它,以便引用字典的值是另一个包含按钮标签和引号的数据结构,而不是使用相同的字符串作为按钮的标签及其名称。

相关问题