Re: adding "Print" menu in wxPython

On Jan 25, 9:51 pm, Akand Islam <sohel...@xxxxxxxxx> wrote:
Thanks for your response. Actually I already know I have to create
"OnPrint" method followed by adding the menu named "Print" that calls
"OnPrint" method. But the problem I am facing is to implement it. Here
are the codes I am adding (from Tutorial) which make an editor and I
want to add printing option so that I can print whatever is written in
the editor. I will appreciate if you add coding with this file.

Well i don't see where you added the methods or the menu command. *
scratches head*

Man I've got to tell you that this code is a horrible example to learn
from! The styling abominations and obfuscation is just simply
atrocious! It seems the author was trying to confuse everyone
including himself! So i modified it just a bit.

I added the OnPrint method however i am leaving it up to you to build
the Print menu and do the binding. I put some comments where you need
to add the print menu. Just look at how the other menus are created
and you'll figure it out ;)

Let us know if you need more help.

import wx
import os.path, sys

class MainWindow(wx.Frame):
def __init__(self, filename='noname.txt'):
wx.Frame.__init__(self, None, size=(400,200))
self.filename = filename
self.dirname = '.'
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)

def CreateMenu(self):
fileMenu = wx.Menu()
item = fileMenu.Append(wx.ID_ABOUT, '&About', 'Info')
self.Bind(wx.EVT_MENU, self.OnAbout, item)
item = fileMenu.Append( wx.ID_OPEN, '&Open', 'Info')
self.Bind(wx.EVT_MENU, self.OnOpen, item)
item = fileMenu.Append(wx.ID_SAVE, '&Save', 'Info')
self.Bind(wx.EVT_MENU, self.OnSave, item)
item = fileMenu.Append(wx.ID_SAVEAS, 'Save &As', 'Info')
self.Bind(wx.EVT_MENU, self.OnSaveAs, item)
# psst: Hey put the print menu here!!!!
# psst: do the binding here!!!!
item = fileMenu.Append(wx.ID_EXIT, 'E&xit', 'Terminate the
self.Bind(wx.EVT_MENU, self.OnExit, item)
menuBar = wx.MenuBar()
menuBar.Append(fileMenu, '&File') # Add the fileMenu to the
self.SetMenuBar(menuBar) # Add the menuBar to the Frame

def SetTitle(self):
# MainWindow.SetTitle overrides wx.Frame.SetTitle, so we have
# call it using super:
super(MainWindow, self).SetTitle('Editor %s'%self.filename)

def defaultFileDialogOptions(self):
''' Return a dictionary with file dialog options that can be
used in both the save file dialog as well as in the open
file dialog. '''
return dict(
message='Choose a file',

def askUserForFilename(self, **dialogOptions):
dialog = wx.FileDialog(self, **dialogOptions)
if dialog.ShowModal() == wx.ID_OK:
userProvidedFilename = True
self.filename = dialog.GetFilename()
self.dirname = dialog.GetDirectory()
self.SetTitle() # Update the window title with the new
userProvidedFilename = False
return userProvidedFilename

# Event handlers:

def OnAbout(self, event):
dialog = wx.MessageDialog(self, 'A sample editor\n'
'in wxPython', 'About Sample Editor', wx.OK)

def OnExit(self, event):
self.Close() # Close the main window.

def OnSave(self, event):
textfile = open(os.path.join(self.dirname, self.filename),

def OnOpen(self, event):
if self.askUserForFilename(
textfile = open(os.path.join(self.dirname, self.filename),

def OnSaveAs(self, event):
if self.askUserForFilename(

def OnPrint(self, event):

app = wx.App()
frame = MainWindow()

#--END CODE--#

Relevant Pages

  • Re: Good practice when writing modules...
    ... def spam: ... My editor has a command that finds and lists me all the global imports ... having your editor trace these dependencies for you is ...
  • Re: Eve - displaying ESC?
    ... DEF KEY=CONTROL-B SET WIDTH 132 ... To me, that's faster than first "teaching" the sequence to the editor, ... Unofficial Affordable OpenVMS Home Page: ... Unofficial OpenVMS Hobbyist Support Page: ...
  • models & editors in PyQt4
    ... I created simple property classes with editing option, but since i'm not too much experienced in PyQt4 i'd like to ask if i handle ColorProperty changing right. ... Any other Property has its own editor and their control flow is imho ok. ... in createEditor(self, parent, option, index) i call QtGui.QColorDialog.getColorand return None ... def createEditor: ...
  • Re: models & editors in PyQt4
    ... Any other Property has its own editor and their control ... Hovewer i'm not sure about ColorProperty. ... You could create an empty placeholder widget in the createEditor ... def createEditor: ...
  • Re: how to define a method template
    ... I will need to write these codes for "apple" ... > def apple_attr= ... Robert I have understood the setup a little bit differently, ...