wxPython:添加一个关于框。

wxPython : Adding an about box.

我正在创建一个 wxPython 音板并且想知道如何实现一个关于框。目标是按下 wxPython File 菜单上的 About 按钮并生成一个 About Box。

到目前为止,这是我的代码:

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

这是我收到的错误:

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

有什么建议吗?

一如既往的感谢,

指挥官000

又是你:)

需要在您缺少的回调中创建 aboutBox OnMenuHelpAbout(如我之前的回答所述,我将其重构为私有)

同样(不相关)但是我已经改变了字典,所以它直接指向声音对象:不再if/elif,你可以加载200个声音代码仍然是一样的。

固定代码:

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

wx python 有自己的 wx.AboutBox()wx.AboutDialogInfo()

结合使用
def About(self, event):
    from platform import platform
    myos = platform()
    aboutInfo = wx.AboutDialogInfo()
    aboutInfo.SetName("My Application ")
    aboutInfo.SetVersion("1.0")
    aboutInfo.SetDescription("My Super App," \
        " That does amazing things\nRunning on: "+myos)
    aboutInfo.SetCopyright("(C) Joe Bloggs-2016")
    aboutInfo.SetLicense("https://www.gnu.org/licenses/gpl-2.0.html")
    aboutInfo.AddDeveloper("Joe Bloggs")
    aboutInfo.AddDocWriter("Joe Bloggs")
    aboutInfo.SetWebSite('https://www.JoeBlogs.com')
    wx.AboutBox(aboutInfo)

参见:https://wxpython.org/docs/api/wx-module.html#AboutBox