adding "Print" menu in wxPython
rantingrick
rantingrick at gmail.com
Wed Jan 26 01:19:28 EST 2011
On Jan 25, 9:51 pm, Akand Islam <sohel... at gmail.com> 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.
#--START CODE--#
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)
self.CreateMenu()
self.CreateStatusBar()
self.SetTitle()
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!!!!
fileMenu.AppendSeparator()
item = fileMenu.Append(wx.ID_EXIT, 'E&xit', 'Terminate the
program')
self.Bind(wx.EVT_MENU, self.OnExit, item)
menuBar = wx.MenuBar()
menuBar.Append(fileMenu, '&File') # Add the fileMenu to the
MenuBar
self.SetMenuBar(menuBar) # Add the menuBar to the Frame
def SetTitle(self):
# MainWindow.SetTitle overrides wx.Frame.SetTitle, so we have
to
# 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',
defaultDir=self.dirname,
wildcard='*.*'
)
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
filename
else:
userProvidedFilename = False
dialog.Destroy()
return userProvidedFilename
# Event handlers:
def OnAbout(self, event):
dialog = wx.MessageDialog(self, 'A sample editor\n'
'in wxPython', 'About Sample Editor', wx.OK)
dialog.ShowModal()
dialog.Destroy()
def OnExit(self, event):
self.Close() # Close the main window.
def OnSave(self, event):
textfile = open(os.path.join(self.dirname, self.filename),
'w')
textfile.write(self.control.GetValue())
textfile.close()
def OnOpen(self, event):
if self.askUserForFilename(
style=wx.OPEN,**self.defaultFileDialogOptions()
):
textfile = open(os.path.join(self.dirname, self.filename),
'r')
self.control.SetValue(textfile.read())
textfile.close()
def OnSaveAs(self, event):
if self.askUserForFilename(
defaultFile=self.filename,
style=wx.SAVE,
**self.defaultFileDialogOptions()
):
self.OnSave(event)
def OnPrint(self, event):
sys.stdout.write(self.control.GetValue())
app = wx.App()
frame = MainWindow()
frame.Show()
app.MainLoop()
#--END CODE--#
More information about the Python-list
mailing list