简体   繁体   English

wxPython并在菜单栏中选择时添加选项菜单

[英]wxPython and add Options Menu when select in Menu Bar

I have set up a wxpython GUI that has a menu bar and some items in the menu bar. 我已经设置了一个wxpython GUI,它有一个菜单栏和菜单栏中的一些项目。 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. 类似的行为将是wx.FontDialog和wx.FileDialog - 但是,我希望我的自定义,因为我可以将单选按钮和复选框作为可选选项。 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): 这里是我设置主应用程序和GUI的一部分(我在另一部分设置了布局和框大小调整器):

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. 我的GUI和菜单栏设置正确 - 我只是不知道如何获得像wx.FileDialog和wx.FontDialog菜单的弹出菜单。 Any help would be great! 任何帮助都会很棒! Thanks 谢谢

You would want to instantiate a dialog in your handler (OnOptimizeOptions). 您可能希望在处理程序中实例化一个对话框(OnOptimizeOptions)。 Basically you would subclass wx.Dialog and put in whatever widgets you want. 基本上你会继承Wx.Dialog并放入你想要的任何小部件。 Then you'd instantiate it in your handler and call ShowModal. 然后你在你的处理程序中实例化它并调用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. 查看zetcodes网站上的自定义对话框部分: http://zetcode.com/wxpython/dialogs/的一个例子(接近底部)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM