首页 > 学院 > 开发设计 > 正文

wxPython+Boa练习程序

2019-11-14 17:43:39
字体:
来源:转载
供稿:网友

最近需要做点支持linux的跨平台gui,网上查到了wxPython及Boa,感觉不错,照着Boa文档做做练习。

代码:

App:

 1 #!/usr/bin/env python 2 #Boa:App:BoaApp 3  4 import wx 5  6 import Frame1 7  8 modules ={'Dialog1': [0, '', u'Dialog1.py'], 9  'Frame1': [1, 'Main frame of application', u'Frame1.py']}10 11 class BoaApp(wx.App):12     def OnInit(self):13         self.main = Frame1.create(None)14         self.main.Show()15         self.SetTopWindow(self.main)16         return True17 18 def main():19     application = BoaApp(0)20     application.MainLoop()21 22 if __name__ == '__main__':23     main()

 

Dialog:

 1 #Boa:Dialog:Dialog1 2  3 import wx 4  5 def create(parent): 6     return Dialog1(parent) 7  8 [wxID_DIALOG1, wxID_DIALOG1BUTTON1, wxID_DIALOG1STATICBITMAP1,  9  wxID_DIALOG1STATICTEXT1, wxID_DIALOG1STATICTEXT2, 10 ] = [wx.NewId() for _init_ctrls in range(5)]11 12 class Dialog1(wx.Dialog):13     def _init_ctrls(self, PRnt):14         # generated method, don't edit15         wx.Dialog.__init__(self, id=wxID_DIALOG1, name=u'Dialog1', parent=prnt,16               pos=wx.Point(365, 232), size=wx.Size(400, 492),17               style=wx.DEFAULT_DIALOG_STYLE, title=u'About Notebook')18         self.SetClientSize(wx.Size(392, 465))19 20         self.staticText1 = wx.StaticText(id=wxID_DIALOG1STATICTEXT1,21               label=u'Note Book - Simple Text Editor', name='staticText1',22               parent=self, pos=wx.Point(72, 32), size=wx.Size(220, 19),23               style=wx.ALIGN_CENTRE)24         self.staticText1.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL,25               False, u'Tahoma'))26 27         self.staticText2 = wx.StaticText(id=wxID_DIALOG1STATICTEXT2,28               label=u'This is my first Boa app.', name='staticText2',29               parent=self, pos=wx.Point(112, 96), size=wx.Size(129, 14),30               style=0)31         self.staticText2.SetBackgroundColour(wx.Colour(212, 208, 200))32 33         self.staticBitmap1 = wx.StaticBitmap(bitmap=wx.Bitmap(u'F:/Projects/guide1/6773383_753857.jpg',34               wx.BITMAP_TYPE_JPEG), id=wxID_DIALOG1STATICBITMAP1,35               name='staticBitmap1', parent=self, pos=wx.Point(48, 152),36               size=wx.Size(280, 160), style=0)37 38         self.button1 = wx.Button(id=wxID_DIALOG1BUTTON1, label=u'Close',39               name='button1', parent=self, pos=wx.Point(152, 328),40               size=wx.Size(75, 24), style=0)41         self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,42               id=wxID_DIALOG1BUTTON1)43 44     def __init__(self, parent):45         self._init_ctrls(parent)46 47     def OnButton1Button(self, event):48         self.Close()

 

Frame: 

  1 #Boa:Frame:Frame1  2   3    4   5 import wx  6 import Dialog1  7   8    9  10 def create(parent): 11     return Frame1(parent) 12  13   14  15 [wxID_FRAME1, wxID_FRAME1STATUSBAR1, wxID_FRAME1TEXTEDITOR,  16 ] = [wx.NewId() for _init_ctrls in range(3)] 17  18   19  20 [wxID_FRAME1MENUFILECLOSE, wxID_FRAME1MENUFILEEXIT, wxID_FRAME1MENUFILEOPEN,  21  wxID_FRAME1MENUFILESAVE, wxID_FRAME1MENUFILESAVEAS,  22 ] = [wx.NewId() for _init_coll_menuFile_Items in range(5)] 23  24   25  26 [wxID_FRAME1MENUHELPABOUT] = [wx.NewId() for _init_coll_menuHelp_Items in range(1)] 27  28   29  30 class Frame1(wx.Frame): 31     def _init_coll_menuBar1_Menus(self, parent): 32         # generated method, don't edit 33  34   35  36         parent.Append(menu=self.menuFile, title=u'File') 37         parent.Append(menu=self.menuHelp, title=u'Help') 38  39   40  41     def _init_coll_menuHelp_Items(self, parent): 42         # generated method, don't edit 43  44   45  46         parent.Append(help=u'Display Info', id=wxID_FRAME1MENUHELPABOUT, 47               kind=wx.ITEM_NORMAL, text=u'About') 48         self.Bind(wx.EVT_MENU, self.OnMenuHelpAboutMenu, 49               id=wxID_FRAME1MENUHELPABOUT) 50  51   52  53     def _init_coll_menuFile_Items(self, parent): 54         # generated method, don't edit 55  56   57  58         parent.Append(help='', id=wxID_FRAME1MENUFILEOPEN, kind=wx.ITEM_NORMAL, 59               text=u'Open') 60         parent.Append(help='', id=wxID_FRAME1MENUFILESAVE, kind=wx.ITEM_NORMAL, 61               text=u'Save') 62         parent.Append(help='', id=wxID_FRAME1MENUFILESAVEAS, 63               kind=wx.ITEM_NORMAL, text=u'Save As') 64         parent.Append(help='', id=wxID_FRAME1MENUFILECLOSE, kind=wx.ITEM_NORMAL, 65               text=u'Close') 66         parent.Append(help='', id=wxID_FRAME1MENUFILEEXIT, kind=wx.ITEM_NORMAL, 67               text=u'Exit') 68         self.Bind(wx.EVT_MENU, self.OnMenuFileOpenMenu, 69               id=wxID_FRAME1MENUFILEOPEN) 70         self.Bind(wx.EVT_MENU, self.OnMenuFileSaveMenu, 71               id=wxID_FRAME1MENUFILESAVE) 72         self.Bind(wx.EVT_MENU, self.OnMenuFileSaveasMenu, 73               id=wxID_FRAME1MENUFILESAVEAS) 74         self.Bind(wx.EVT_MENU, self.OnMenuFileCloseMenu, 75               id=wxID_FRAME1MENUFILECLOSE) 76         self.Bind(wx.EVT_MENU, self.OnMenuFileExitMenu, 77               id=wxID_FRAME1MENUFILEEXIT) 78  79   80  81     def _init_coll_statusBar1_Fields(self, parent): 82         # generated method, don't edit 83         parent.SetFieldsCount(1) 84  85   86  87         parent.SetStatusText(number=0, text=u'status') 88  89   90  91         parent.SetStatusWidths([-1]) 92  93   94  95     def _init_utils(self): 96         # generated method, don't edit 97         self.menuFile = wx.Menu(title=u'File') 98  99  100 101         self.menuHelp = wx.Menu(title=u'Help')102 103  104 105         self.menuBar1 = wx.MenuBar()106 107  108 109         self._init_coll_menuFile_Items(self.menuFile)110         self._init_coll_menuHelp_Items(self.menuHelp)111         self._init_coll_menuBar1_Menus(self.menuBar1)112 113  114 115     def _init_ctrls(self, prnt):116         # generated method, don't edit117         wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,118               pos=wx.Point(550, 227), size=wx.Size(400, 492),119               style=wx.DEFAULT_FRAME_STYLE, title=u'Notebook')120         self._init_utils()121         self.SetClientSize(wx.Size(392, 465))122         self.SetToolTipString(u'Frame1')123         self.SetWindowVariant(wx.WINDOW_VARIANT_LARGE)124         self.SetMenuBar(self.menuBar1)125 126  127 128         self.statusBar1 = wx.StatusBar(id=wxID_FRAME1STATUSBAR1,129               name='statusBar1', parent=self, style=0)130         self._init_coll_statusBar1_Fields(self.statusBar1)131         self.SetStatusBar(self.statusBar1)132 133  134 135         self.textEditor = wx.TextCtrl(id=wxID_FRAME1TEXTEDITOR,136               name=u'textEditor', parent=self, pos=wx.Point(0, 0),137               size=wx.Size(392, 426), style=wx.TE_MULTILINE, value=u'')138 139  140 141     def __init__(self, parent):142         self._init_ctrls(parent)143         self.FileName = None144 145  146 147     def OnMenuFileOpenMenu(self, event):148         dlg = wx.FileDialog(self, 'Choose a file', '.', '', '*.*', wx.OPEN)149         try:150             if dlg.ShowModal() == wx.ID_OK:151                 filename = dlg.GetPath()152                 # Your code153                 self.textEditor.LoadFile(filename)154                 self.FileName = filename155                 self.SetTitle(('Notebook - %s') % filename)156         finally:157             dlg.Destroy()       158 159  160 161     def OnMenuFileSaveMenu(self, event):162         if self.FileName == None:163             return self.OnFileSaveasMenu(event)164         else:165             self.textEditor.SaveFile(self.FileName)166         167     def OnMenuFileCloseMenu(self, event):168         self.FileName = None169         self.textEditor.clear()170         self.SetTitle('Notebook')       171 172  173 174     def OnMenuFileExitMenu(self, event):175         self.Close()      176 177  178 179     def OnMenuHelpAboutMenu(self, event):180         dlg = Dialog1.Dialog1(self)181         try:182             dlg.ShowModal()183         finally:184             dlg.Destroy()185 186  187 188     def OnMenuFileSaveasMenu(self, event):189         dlg = wx.FileDialog(self, 'Save file as', '.', '', '*.*', wx.SAVE)190         try:191             if dlg.ShowModal() == wx.ID_OK:192                 filename = dlg.GetPath()193                 # Your code194                 self.textEditor.SaveFile(filename)195                 self.FileName = filename196                 self.SetTitle(('Notebook - %s') % filename)                197         finally:198             dlg.Destroy()

 

运行结果图:


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表