wxPython:添加一个about框。

时间:2016-08-20 21:15:27

标签: python wxpython

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

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

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'

有什么建议吗?

一如既往地谢谢,

kommander000

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

你再次:)

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

另外(不相关)但我更改了字典,因此它直接指向声音对象:不再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()