asim ali
asim ali

Reputation: 3

Show different panel for each menu click using python library wxpython

Python code it show menubar with menu button

import wx
#dashboard frame
class mainGUI(wx.Frame):
    def __init__(self,parent,title):
        wx.Frame.__init__(self,parent,title=title,size=(1024,780))
        self.initialise()
    def initialise(self):
        panel=wx.Panel(self)
        menubar=wx.MenuBar()
        #buttons for  menu
        home=wx.Menu()
        report=wx.Menu()
        statics=wx.Menu()
        data=wx.Menu()
        chart=wx.Menu()

        #appending button to the menubar
        #here should be menu event handler for each panel to show       
        menubar.Append(home,"Home")
        menubar.Append(report,"report")
        menubar.Append(statics,"statics")
        menubar.Append(data,"data") 
        menubar.Append(chart,"chart")
        self.SetMenuBar(menubar)

Classes should be here for each panel 

    #Attaching the event handler for each menu 
    self.Show(True)

Upvotes: 0

Views: 73

Answers (1)

Mike
Mike

Reputation: 3

To allow code to recognize which menu was clicked you need to add:

self.Bind(wx.EVT_MENU_OPEN, self.OnMenu, menubar)

which will trigger OnMenu method.

Within method you can recognize which menu was clicked with event query:

evt.Menu.Title

From there you can change currently displayed panel.

import wx
class MainGUI(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(1024, 780))
        self.initialise()

    def initialise(self):

        menubar = wx.MenuBar()
        home = wx.Menu()
        report = wx.Menu()
        menubar.Append(home, "Home")
        menubar.Append(report, "report")

        self.Bind(wx.EVT_MENU_OPEN, self.OnMenu, menubar)

        self.SetMenuBar(menubar)

    def OnMenu(self, evt=None):
        if evt.Menu.Title == 'Home':
            print('Home menu clicked')
        elif evt.Menu.Title == 'report':
            print('report menu clicked')


class MainApp(wx.App):
    def __init__(self):
        wx.App.__init__(self)

        main_window = MainGUI(None, title='My app')
        main_window.Show()


def main():
    app = MainApp()
    app.MainLoop()


if __name__ == '__main__':
    main()

Upvotes: 0

Related Questions