0

I have set up a wxpython GUI that has a menu bar and some items in the menu bar. What I would like to do is select an item in my menu bar (ex. File - Options), and when i select "Options" have a dialog box pop up in which I can set different parameters in my code. Similar behaviors would be wx.FontDialog and wx.FileDialog -- However, I want mine to be custom in that i could have radio buttons and check boxes as selectable options. How do I do this?

Snippets of my code are:

Here is where I set up part of the Main application and GUI (I have layout and box sizers set up in another section):

class TMainForm(wx.Frame):

    def __init__(self, *args, **kwds):

            kwds["style"] = wx.DEFAULT_FRAME_STYLE
            wx.Frame.__init__(self, *args, **kwds)
            self.Splitter = wx.SplitterWindow(self, -1, style=wx.SP_3D|wx.SP_BORDER)
            self.PlotPanel = wx.Panel(self.Splitter, -1)
            self.FilePanel = wx.Panel(self.Splitter, -1)
            #self.SelectionPanel = wx.Panel(self.Splitter,-1)
            self.Notebook = wx.Notebook(self.FilePanel, -1)#, style=0)
            self.ReportPage = wx.Panel(self.Notebook, -1)
            self.FilePage = wx.Panel(self.Notebook, -1)

Here is where I set up part of the Menu Bar:

            self.MainMenu = wx.MenuBar()
            self.FileMenu = wx.Menu()
            self.OptimizeMenu = wx.Menu()
            self.HelpMenu = wx.Menu()
            self.OptimizeOptions= wx.MenuItem(self.OptimizeMenu, 302, "&Select Parameters","Select Parameters for Optimization",wx.ITEM_NORMAL)
            self.OptimizeMenu.AppendItem(self.OptimizeOptions)

            self.MainMenu.Append(self.OptimizeMenu, "&Optimization")

Here is where I bind an event to my "options" part of my menu bar. When I click on this, I want a pop up menu dialog to show up

self.Bind(wx.EVT_MENU, self.OnOptimizeOptions, self.OptimizeOptions)

This is the function in which i'm hoping the pop up menu will be defined. I would like to do it in this format if possible (rather than doing separate classes).

def OnOptimizeOptions(self,event):
        give me a dialog box (radio buttons, check boxes, etc)

I have only shown snippets, but all of my code does work. My GUI and menu bars are set up correctly - i just don't know how to get a pop up menu like the wx.FileDialog and wx.FontDialog menus. Any help would be great! Thanks

1 Answer 1

3

You would want to instantiate a dialog in your handler (OnOptimizeOptions). Basically you would subclass wx.Dialog and put in whatever widgets you want. Then you'd instantiate it in your handler and call ShowModal. Something like this psuedo-code:

myDlg = MyDialog(*args)
myDlg.ShowModal()

See the Custom Dialog part on zetcodes site: http://zetcode.com/wxpython/dialogs/ (near the bottom) for one example.

EDIT - Here's an example:

import wx

########################################################################
class MyDialog(wx.Dialog):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Dialog.__init__(self, None, title="Options")

        radio1 = wx.RadioButton( self, -1, " Radio1 ", style = wx.RB_GROUP )
        radio2 = wx.RadioButton( self, -1, " Radio2 " )
        radio3 = wx.RadioButton( self, -1, " Radio3 " )

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(radio1, 0, wx.ALL, 5)
        sizer.Add(radio2, 0, wx.ALL, 5)
        sizer.Add(radio3, 0, wx.ALL, 5)

        for i in range(3):
            chk = wx.CheckBox(self, label="Checkbox #%s" % (i+1))
            sizer.Add(chk, 0, wx.ALL, 5)
        self.SetSizer(sizer)


########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "wx.Menu Tutorial")

        # Add a panel so it looks the correct on all platforms
        self.panel = wx.Panel(self, wx.ID_ANY)

        menuBar = wx.MenuBar()
        fileMenu = wx.Menu()

        optionsItem = fileMenu.Append(wx.NewId(), "Options", 
                                      "Show an Options Dialog")
        self.Bind(wx.EVT_MENU, self.onOptions, optionsItem)

        exitMenuItem = fileMenu.Append(wx.NewId(), "Exit",
                                       "Exit the application")
        self.Bind(wx.EVT_MENU, self.onExit, exitMenuItem)

        menuBar.Append(fileMenu, "&File")
        self.SetMenuBar(menuBar)

    #----------------------------------------------------------------------
    def onExit(self, event):
        """"""
        self.Close()

    #----------------------------------------------------------------------
    def onOptions(self, event):
        """"""
        dlg = MyDialog()
        dlg.ShowModal()
        dlg.Destroy()

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I'm pretty new to wxpython. You say to subclass wx.Dialog -- does this mean something like class Example(wx.Frame) and then all the defs below this? I'm not sure what the myDlg = MyDialog(*args) is quite looking for. One of the main things i'm not understanding about Classes as well is the use of def main(): ex = wx.App() Example(None) ex.MainLoop() Can you give more detail -- I'm very interested in understanding this all more and using it correctly
I added an example that should help you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.