Search code examples
pythonwxpython

wxPython : Adding an about box.


I'm creating a wxPython soundboard and was wondering how to implement an About Box. The goal is to press an About button on the wxPython File menu and generate an About Box.

This is my code so far:

import wx
import os
import pygame

 pygame.init()

 ##SOUNDS##
 goliathwav = pygame.mixer.Sound("goliath.wav")
 channelopen = pygame.mixer.Sound("channelopen.wav")
 ##SOUNDS##


class windowClass(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(windowClass,self).__init__(*args,**kwargs)
        self.__basicGUI()
    def __basicGUI(self):
        panel = wx.Panel(self)
        menuBar = wx.MenuBar()
        fileButton = wx.Menu()
        editButton = wx.Menu()
        aboutBox = wx.MessageDialog(None, "Created by      Kommander000(cyrex)")
        answer=aboutBox.ShowModal()
        aboutBox.Destroy()
        aboutButton = wx.Menu()
        exitItem = fileButton.Append(wx.ID_EXIT, 'Exit','status msg...')
        aboutItem = aboutButton.Append(wx.ID_ABOUT, "About")



        menuBar.Append(fileButton, 'File')
        menuBar.Append(editButton, 'Edit')
        menuBar.Append(aboutButton, 'About this program')

        self.SetMenuBar(menuBar)
        self.Bind(wx.EVT_MENU, self.__quit, exitItem)
        self.Bind(wx.EVT_MENU, self.OnMenuHelpAbout, aboutBox)

        self.__sound_dict = { "Goliath" : "goliath.wav",
                              "Goliath2" : "channelopen.wav"
                            }

        self.__sound_list = sorted(self.__sound_dict.keys())

        self.__list = wx.ListBox(panel,pos=(20,20), size=(250,150))
        for i in self.__sound_list:
            self.__list.Append(i)
        self.__list.Bind(wx.EVT_LISTBOX,self.__on_click)

        #wx.TextCtrl(panel,pos=(10,10), size=(250,150))

        self.SetTitle("Soundboard")
       self.Show(True)

    def __on_click(self,event):
        event.Skip()
        name = self.__sound_list[self.__list.GetSelection()]
        filename = self.__sound_dict[name]
        if filename == "goliath.wav":
             print "[ NOW PLAYING ] ... %s" % filename
             pygame.mixer.Sound.play(goliathwav)
        if filename == "channelopen.wav":
            print "[ NOW PLAYING ] ... %s" % filename
            pygame.mixer.Sound.play(channelopen)


    def __quit(self, e):
         self.Close()
def main():
    app = wx.App()
    windowClass(None, -1, style=wx.MAXIMIZE_BOX | wx.CAPTION | wx.CENTRE)
    app.MainLoop()

 main()

This is the error I am receiving:

self.Bind(wx.EVT_MENU, self.OnMenuHelpAbout, aboutBox)
AttributeError: 'windowClass' object has no attribute 'OnMenuHelpAbout'

Any suggestions?

Thanks as always,

kommander000


Solution

  • you again :)

    The aboutBox needs to be created in your missing callback OnMenuHelpAbout (that I refactored to make private as stated in my previous answer)

    Also (not related) but I have changed the dictionary so it directly points to the sound object: no more if/elif, you can load 200 sounds the code will still be the same.

    Fixed code:

    import wx
    import os
    import pygame
    
    pygame.init()
    
     ##SOUNDS##
     ##SOUNDS##
    
    
    class windowClass(wx.Frame):
        __goliathwav = pygame.mixer.Sound("goliath.wav")
        __channelopen = pygame.mixer.Sound("channelopen.wav")
    
        def __init__(self, *args, **kwargs):
            super(windowClass,self).__init__(*args,**kwargs)
            self.__basicGUI()
        def __basicGUI(self):
            panel = wx.Panel(self)
            menuBar = wx.MenuBar()
            fileButton = wx.Menu()
            editButton = wx.Menu()
            aboutButton = wx.Menu()
            exitItem = fileButton.Append(wx.ID_EXIT, 'Exit','status msg...')
            aboutItem = aboutButton.Append(wx.ID_ABOUT, "About")
    
    
    
            menuBar.Append(fileButton, 'File')
            menuBar.Append(editButton, 'Edit')
            menuBar.Append(aboutButton, 'About this program')
    
            self.SetMenuBar(menuBar)
            self.Bind(wx.EVT_MENU, self.__quit, exitItem)
            self.Bind(wx.EVT_MENU, self.__onmenuhelpabout, aboutItem)
    
            self.__sound_dict = { "Goliath" : self.__goliathwav,
                                  "Goliath2" : self.__channelopen
                                }
    
            self.__sound_list = sorted(self.__sound_dict.keys())
    
            self.__list = wx.ListBox(panel,pos=(20,20), size=(250,150))
            for i in self.__sound_list:
                self.__list.Append(i)
            self.__list.Bind(wx.EVT_LISTBOX,self.__on_click)
    
            #wx.TextCtrl(panel,pos=(10,10), size=(250,150))
    
            self.SetTitle("Soundboard")
            self.Show(True)
    
        def __onmenuhelpabout(self,event):
            event.Skip()
            aboutBox = wx.MessageDialog(None, "Created by      Kommander000(cyrex)")
            answer=aboutBox.ShowModal()
            aboutBox.Destroy()
    
        def __on_click(self,event):
            event.Skip()
            name = self.__sound_list[self.__list.GetSelection()]
            sound = self.__sound_dict[name]
            print("[ NOW PLAYING ] ... %s" % name)
            pygame.mixer.Sound.play(sound)
    
    
        def __quit(self, e):
             self.Close()
    def main():
        app = wx.App()
        windowClass(None, -1, style=wx.MAXIMIZE_BOX | wx.CAPTION | wx.CENTRE)
        app.MainLoop()
    
    main()
        def __quit(self, e):
             self.Close()
    def main():
        app = wx.App()
        windowClass(None, -1, style=wx.MAXIMIZE_BOX | wx.CAPTION | wx.CENTRE)
        app.MainLoop()
    
    main()