Why I have not seen any underline with the menu items although I have put "&" within the menu names. My code is as follows:

import wx
import os


class MainWindow(wx.Frame):
    def __init__(self, parent):
        self.dirname=''

        # A "-1" in the size parameter instructs wxWidgets to use the default size.
        # In this case, we select 200px width and the default height.
        wx.Frame.__init__(self, parent, size=(200,-1))
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
        self.CreateStatusBar() # A Statusbar in the bottom of the window

        # Setting up the menu.
        filemenu= wx.Menu()
        menuOpen = filemenu.Append(wx.ID_OPEN, "&Opent\tCrtl+O"," Open a file to edit")
        menuAbout= filemenu.Append(wx.ID_ABOUT, "&About\tCrtl+A"," Information about this program")
        menuExit = filemenu.Append(wx.ID_EXIT,"E&xit\tAlt+F4"," Terminate the program")

        # Creating the menubar.
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"File") # Adding the "filemenu" to the MenuBar
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.

        # Events.
        self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)

        
        # Accelerate Table
        op_id = wx.NewId()
        ab_id = wx.NewId()
        self.Bind(wx.EVT_MENU, self.OnOpen, id=op_id)
        self.Bind(wx.EVT_MENU, self.OnAbout, id=ab_id)
        self.accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('O'), op_id),
                                                   (wx.ACCEL_CTRL, ord('A'), ab_id)])
        self.SetAcceleratorTable(self.accel_tbl)
        # To close window
        self.Bind(wx.EVT_CLOSE, self.OnExit, menuExit)

        self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        self.buttons = []
        for i in range(0, 6):
            self.buttons.append(wx.Button(self, -1, "Button &"+str(i)))
            self.sizer2.Add(self.buttons[i], 1, wx.EXPAND)

        ## Use some sizers to see layout options
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.control, 1, wx.EXPAND)
        self.sizer.Add(self.sizer2, 0, wx.EXPAND)

        ##Layout sizers
        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)
        self.Show()

    def OnAbout(self,e):
        # Create a message dialog box
        dlg = wx.MessageDialog(self, " Wahid \n in wxPython", "About Sample Editor", wx.OK)
        dlg.ShowModal() # Shows it
        dlg.Destroy() # finally destroy it when finished.

    def OnExit(self,e):
        self.Close(True)  # Close the frame.

    def OnOpen(self,e):
        """Open a file"""
        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            f = open(os.path.join(self.dirname, self.filename), 'r')
            self.control.SetValue(f.read())
            f.close()
        dlg.Destroy()

app = wx.App(False)
frame = MainWindow(None)
app.MainLoop()

My output sample is in attachment. I will appreciate if some one can give me solution.

Thanks in advance.
-- Akand

Attachments sample.png 27.53 KB

On my Ubuntu linux Box There is nothing wrong with your code. the underline is seen.

add this line after line 24

self.SetManubar(menuBar)
menuBar.Realize()

after that.

This help on windows ok.
;)

Thanks for your response. But in this case, I have got error message:

AttributeError: 'MenuBar' object has no attribute 'Realize'

The code is:

import wx
import os


class MainWindow(wx.Frame):
    def __init__(self, parent):
        self.dirname=''

        # A "-1" in the size parameter instructs wxWidgets to use the default size.
        # In this case, we select 200px width and the default height.
        wx.Frame.__init__(self, parent, size=(200,-1))
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
        self.CreateStatusBar() # A Statusbar in the bottom of the window

        # Setting up the menu.
        filemenu= wx.Menu()
        menuOpen = filemenu.Append(wx.ID_OPEN, "&Opent\tCrtl+O"," Open a file to edit")
        menuAbout= filemenu.Append(wx.ID_ABOUT, "&About\tCrtl+A"," Information about this program")
        menuExit = filemenu.Append(wx.ID_EXIT,"E&xit\tAlt+F4"," Terminate the program")

        # Creating the menubar.
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"File") # Adding the "filemenu" to the MenuBar
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
        menuBar.Realize()

        # Events.
        self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)

        
        # Accelerate Table
        op_id = wx.NewId()
        ab_id = wx.NewId()
        self.Bind(wx.EVT_MENU, self.OnOpen, id=op_id)
        self.Bind(wx.EVT_MENU, self.OnAbout, id=ab_id)
        self.accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('O'), op_id),
                                                   (wx.ACCEL_CTRL, ord('A'), ab_id)])
        self.SetAcceleratorTable(self.accel_tbl)
        # To close window
        self.Bind(wx.EVT_CLOSE, self.OnExit, menuExit)

        self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        self.buttons = []
        for i in range(0, 6):
            self.buttons.append(wx.Button(self, -1, "Button &"+str(i)))
            self.sizer2.Add(self.buttons[i], 1, wx.EXPAND)

        ## Use some sizers to see layout options
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.control, 1, wx.EXPAND)
        self.sizer.Add(self.sizer2, 0, wx.EXPAND)

        ##Layout sizers
        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)
        self.Show()

    def OnAbout(self,e):
        # Create a message dialog box
        dlg = wx.MessageDialog(self, " Wahid \n in wxPython", "About Sample Editor", wx.OK)
        dlg.ShowModal() # Shows it
        dlg.Destroy() # finally destroy it when finished.

    def OnExit(self,e):
        self.Close(True)  # Close the frame.

    def OnOpen(self,e):
        """Open a file"""
        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            f = open(os.path.join(self.dirname, self.filename), 'r')
            self.control.SetValue(f.read())
            f.close()
        dlg.Destroy()

app = wx.App(False)
frame = MainWindow(None)
app.MainLoop()

-- Akand

Yea sorry mate. Realize is for toolbar. Is my mistake. This happends when dealing with so manythings at one time. I will run your code on windows box now.

Dear Richieking,
Please let me know when you have checked in your windows box. I will be waiting since the problem is not solved yet.

-- Akand

Sorry mate but this is windows thing not wxpython.

Check this out....

Right click the Desktop/Properties/Appearance/Effects/Hide underlined
letters

And you can toggle the menu-underline by pressing ALT tab.

Thanks for response. My system is Windows 7. Therefore I could not figure out what to do according to your suggestion.

-- Akand

Thanks for response. What does mean toggling the menu-underline pressing ALT tab? I mean, when I have to press ALT tab?

-- Akand

just start your wxpy app. and when you see your window. just hit ALT tab are the menu underline will be visible.

Thanks a lot. From my curiosity, is this the way menu-underline visibility works? It seems temporary though.

This question has already been answered. Start a new discussion instead.