Gigiux
Gigiux

Reputation: 144

bind menu events wxpython

I am new in Python, but I would like to understand the use of GUIs using wxpython. I am using a template for the creation of a frame and I added the menus. The menus are shown but they do not trigger any action, so I need to bind actions to the different menu items I created. Problem is, I don't know how.

I started with the menu save, which I called 'menu_open' and associated to the method

filemenu.Append(wx.ID_OPEN, "Open")

I associated an action using:

self.Bind(wx.EVT_MENU, self.Open, menu_open)

but I got the error:

AttributeError: 'MainWindow' object has no attribute 'Open'

If I try with 'OnOpen' (since there is an 'OnExit' attribute) I get the errors:

frame = MainWindow(None, "Sample editor")

AttributeError: 'MainWindow'object has no attribute 'OnOpen'

So the questions are:

  1. is the self.Bind syntax correct and the right way to assign an action to a menu?
  2. is there a complete list of attributes for the menus available in wxPython?

I am reporting the whole code for reference. Thanks. G.

#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import print_function

import wx


class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(200, 100))
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
        # A Statusbar in the bottom of the window
        self.CreateStatusBar()

        # Setting up the menus
        '''Define main items'''
        filemenu = wx.Menu()
        editmenu = wx.Menu()
        infomenu = wx.Menu()
        '''Items'''
        # file menu
        menu_open = filemenu.Append(wx.ID_OPEN, "Open")
        filemenu.Append(wx.ID_NEW, "New")
        filemenu.Append(wx.ID_SAVE, "Save")
        filemenu.Append(wx.ID_SAVEAS, "Save as")
        filemenu.Append(wx.ID_EXIT, "Exit")
        filemenu.AppendSeparator()
        filemenu.Append(wx.ID_PRINT, "&Print")
        filemenu.Append(wx.ID_PRINT_SETUP, "Print setup")
        filemenu.Append(wx.ID_PREVIEW, "Preview")
        # edit menu
        editmenu.Append(wx.ID_COPY, "Copy")
        editmenu.Append(wx.ID_CUT, "Cut")
        editmenu.Append(wx.ID_PASTE, "Paste")
        editmenu.AppendSeparator()
        editmenu.Append(wx.ID_UNDO, "Undo")
        editmenu.Append(wx.ID_REDO, "Re-do it")
        # info menu
        infomenu.Append(wx.ID_ABOUT, "About")
        '''Bind items for activation'''
        # bind file menu
        self.Bind(wx.EVT_MENU, self.OnOpen, menu_open)

        # Creating the menubar.
        menuBar = wx.MenuBar()
        # Add menus
        menuBar.Append(filemenu, "&File")
        menuBar.Append(editmenu, "&Edit")
        menuBar.Append(infomenu, "&Help")
        # Adding the MenuBar to the Frame content.
        self.SetMenuBar(menuBar)
        self.Show(True)

app = wx.App(False)
frame = MainWindow(None, "Sample editor")
app.MainLoop()

Upvotes: 0

Views: 2674

Answers (1)

Yoriz
Yoriz

Reputation: 3625

You simply have not created the event handler method so when using

self.Bind(wx.EVT_MENU, self.OnOpen, menu_open)

you need a method that will be called added to the class MainWindow

def OnOpen(self, event):
    print('OnOpen')

Upvotes: 2

Related Questions