Recently, we need to develop a cross-platform gui that supports linux. I found wxPython and Boa on the Internet. It feels good. I will do exercises according to the Boa document.
Code:
App:
#! /Usr/bin/env python
# Boa: App: BoaApp
Import wx
Import Frame1
Modules = {'dialog1 ': [0, '', u'dialog1. py'],
'Frame1': [1, 'main frame of application', u'frame1. py']}
Class BoaApp (wx. App ):
Def OnInit (self ):
Self. main = Frame1.create (None)
Self. main. Show ()
Self. SetTopWindow (self. main)
Return True
Def main ():
Application = BoaApp (0)
Application. MainLoop ()
If _ name _ = '_ main __':
Main ()
Dialog:
# Boa: Dialog: Dialog1
Import wx
Def create (parent ):
Return Dialog1 (parent)
[WxID_DIALOG1, wxID_DIALOG1BUTTON1, wxID_DIALOG1STATICBITMAP1,
WxID_DIALOG1STATICTEXT1, wxID_DIALOG1STATICTEXT2,
] = [Wx. NewId () for _ init_ctrls in range (5)]
Class Dialog1 (wx. Dialog ):
Def _ init_ctrls (self, prnt ):
# Generated method, don't edit
Wx. Dialog. _ init _ (self, id = wxID_DIALOG1, name = u'dialog1 ', parent = prnt,
Pos = wx. Point (365,232), size = wx. Size (400,492 ),
Style = wx. DEFAULT_DIALOG_STYLE, title = u'about NOTEBOOK ')
Self. SetClientSize (wx. Size (392,465 ))
Self. staticText1 = wx. StaticText (id = wxID_DIALOG1STATICTEXT1,
Label = u'note Book-Simple Text editor', name = 'statictext1 ',
Parent = self, pos = wx. Point (72, 32), size = wx. Size (220, 19 ),
Style = wx. ALIGN_CENTRE)
Self. staticText1.SetFont (wx. Font (12, wx. SWISS, wx. NORMAL, wx. NORMAL,
False, u 'tahoma '))
Self. staticText2 = wx. StaticText (id = wxID_DIALOG1STATICTEXT2,
Label = u'this is my first Boa app. ', name = 'statictext2 ',
Parent = self, pos = wx. Point (112, 96), size = wx. Size (129, 14 ),
Style = 0)
Self. staticText2.SetBackgroundColour (wx. Colour (212,208,200 ))
Self. staticBitmap1 = wx. StaticBitmap (bitmap = wx. Bitmap (u 'f:/Projects/guide1/6773383_752.167.jpg ',
Wx. BITMAP_TYPE_JPEG), id = wxID_DIALOG1STATICBITMAP1,
Name = 'staticbitmap1', parent = self, pos = wx. Point (48,152 ),
Size = wx. Size (280,160), style = 0)
Self. button1 = wx. Button (id = wxID_DIALOG1BUTTON1, label = u'close ',
Name = 'ton1', parent = self, pos = wx. Point (152,328 ),
Size = wx. Size (75, 24), style = 0)
Self. button1.Bind (wx. EVT_BUTTON, self. OnButton1Button,
Id = wxID_DIALOG1BUTTON1)
Def _ init _ (self, parent ):
Self. _ init_ctrls (parent)
Def OnButton1Button (self, event ):
Self. Close ()
Frame:
# Boa: Frame: Frame1
Import wx
Import Dialog1
Def create (parent ):
Return Frame1 (parent)
[WxID_FRAME1, wxID_FRAME1STATUSBAR1, wxID_FRAME1TEXTEDITOR,
] = [Wx. NewId () for _ init_ctrls in range (3)]
[WxID_FRAME1MENUFILECLOSE, wxID_FRAME1MENUFILEEXIT, wxID_FRAME1MENUFILEOPEN,
WxID_FRAME1MENUFILESAVE, wxID_FRAME1MENUFILESAVEAS,
] = [Wx. NewId () for _ init_coll_menuFile_Items in range (5)]
[WxID_FRAME1MENUHELPABOUT] = [wx. NewId () for _ init_coll_menuHelp_Items in range (1)]
Class Frame1 (wx. Frame ):
Def _ init_coll_menuBar1_Menus (self, parent ):
# Generated method, don't edit
Parent. Append (menu = self. menuFile, title = u'file ')
Parent. Append (menu = self. menuHelp, title = u'help ')
Def _ init_coll_menuHelp_Items (self, parent ):
# Generated method, don't edit
Parent. Append (help = u 'display info', id = wxID_FRAME1MENUHELPABOUT,
Kind = wx. ITEM_NORMAL, text = u 'about ')
Self. Bind (wx. EVT_MENU, self. OnMenuHelpAboutMenu,
Id = wxID_FRAME1MENUHELPABOUT)
Def _ init_coll_menuFile_Items (self, parent ):
# Generated method, don't edit
Parent. Append (help = '', id = wxID_FRAME1MENUFILEOPEN, kind = wx. ITEM_NORMAL,
Text = u'open ')
Parent. Append (help = '', id = wxID_FRAME1MENUFILESAVE, kind = wx. ITEM_NORMAL,
Text = u'save ')
Parent. Append (help = '', id = wxID_FRAME1MENUFILESAVEAS,
Kind = wx. ITEM_NORMAL, text = u 'save ')
Parent. Append (help = '', id = wxID_FRAME1MENUFILECLOSE, kind = wx. ITEM_NORMAL,
Text = u'close ')
Parent. Append (help = '', id = wxID_FRAME1MENUFILEEXIT, kind = wx. ITEM_NORMAL,
Text = u'exit ')
Self. Bind (wx. EVT_MENU, self. OnMenuFileOpenMenu,
Id = wxID_FRAME1MENUFILEOPEN)
Self. Bind (wx. EVT_MENU, self. OnMenuFileSaveMenu,
Id = wxID_FRAME1MENUFILESAVE)
Self. Bind (wx. EVT_MENU, self. OnMenuFileSaveasMenu,
Id = wxID_FRAME1MENUFILESAVEAS)
Self. Bind (wx. EVT_MENU, self. OnMenuFileCloseMenu,
Id = wxID_FRAME1MENUFILECLOSE)
Self. Bind (wx. EVT_MENU, self. OnMenuFileExitMenu,
Id = wxID_FRAME1MENUFILEEXIT)
Def _ init_coll_statusBar1_Fields (self, parent ):
# Generated method, don't edit
Parent. SetFieldsCount (1)
Parent. SetStatusText (number = 0, text = u 'status ')
Parent. SetStatusWidths ([-1])
Def _ init_utils (self ):
# Generated method, don't edit
Self. menuFile = wx. Menu (title = u'file ')
Self. menuHelp = wx. Menu (title = u'help ')
Self. menuBar1 = wx. MenuBar ()
Self. _ init_coll_menuFile_Items (self. menuFile)
Self. _ init_coll_menuHelp_Items (self. menuHelp)
Self. _ init_coll_menubarw.menus (self. menuBar1)
Def _ init_ctrls (self, prnt ):
# Generated method, don't edit
Wx. Frame. _ init _ (self, id = wxID_FRAME1, name = '', parent = prnt,
Pos = wx. Point (550,227), size = wx. Size (400,492 ),
Style = wx. DEFAULT_FRAME_STYLE, title = u 'notebook ')
Self. _ init_utils ()
Self. SetClientSize (wx. Size (392,465 ))
Self. SetToolTipString (u'frame1 ')
Self. SetWindowVariant (wx. WINDOW_VARIANT_LARGE)
Self. SetMenuBar (self. menuBar1)
Self. statusBar1 = wx. StatusBar (id = wxID_FRAME1STATUSBAR1,
Name = 'statusbar1', parent = self, style = 0)
Self. _ init_coll_statusBar1_Fields (self. statusBar1)
Self. SetStatusBar (self. statusBar1)
Self. textEditor = wx. TextCtrl (id = wxID_FRAME1TEXTEDITOR,
Name = u'texteditor ', parent = self, pos = wx. Point (0, 0 ),
Size = wx. Size (392,426), style = wx. TE_MULTILINE, value = u '')
Def _ init _ (self, parent ):
Self. _ init_ctrls (parent)
Self. FileName = None
Def OnMenuFileOpenMenu (self, event ):
Dlg = wx. FileDialog (self, 'Choose a file', '.', '', '*. *', wx. OPEN)
Try:
If dlg. ShowModal () = wx. ID_ OK:
Filename = dlg. GetPath ()
# Your code
Self. textEditor. LoadFile (filename)
Self. FileName = filename
Self. SetTitle ('notebook-% s') % filename)
Finally:
Dlg. Destroy ()
Def OnMenuFileSaveMenu (self, event ):
If self. FileName = None:
Return self. OnFileSaveasMenu (event)
Else:
Self. textEditor. SaveFile (self. FileName)
Def OnMenuFileCloseMenu (self, event ):
Self. FileName = None
Self. textEditor. clear ()
Self. SetTitle ('notebook ')
Def OnMenuFileExitMenu (self, event ):
Self. Close ()
Def OnMenuHelpAboutMenu (self, event ):
Dlg = Dialog1.Dialog1 (self)
Try:
Dlg. ShowModal ()
Finally:
Dlg. Destroy ()
Def OnMenuFileSaveasMenu (self, event ):
Dlg = wx. FileDialog (self, 'Save file as ','. ', '',' *. * ', wx. Save)
Try:
If dlg. ShowModal () = wx. ID_ OK:
Filename = dlg. GetPath ()
# Your code
Self. textEditor. SaveFile (filename)
Self. FileName = filename
Self. SetTitle ('notebook-% s') % filename)
Finally:
Dlg. Destroy ()
Running result diagram: