【问题标题】:wxPython Buttons and Pop up MessagewxPython 按钮和弹出消息
【发布时间】:2015-07-14 01:47:08
【问题描述】:

我正在创建一个列出按钮行的 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()

【问题讨论】:

    标签: python-2.7 button dialog wxpython panel


    【解决方案1】:

    创建一系列按钮并将它们绑定到同一个处理程序是相当容易的。几年前我写过这个话题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()
    

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

    【讨论】:

      猜你喜欢
      • 2014-02-08
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多