1 # /Usr/bin/Python 2 # -*-<Coding = UTF-8> -*- 3 4 """ 5 This example is a basic wxpython GUI.ProgramIt mainly shows how the file dialog box works. 6 """ 7 8 Import WX 9 Import OS 10 11 Class Guimainframe (wx. Frame ): 12 13 Def _ Init __ (Self ): 14 Wx. Frame. _ Init __ (Self, parent = none, id =-1, Title = "" , Pos = wx. defaultposition, size = Wx. defaultsize) 15 Panel = Wx. Panel (Self) 16 Panel. setbackgroundcolour ( " White " ) 17 18 # Menu Bar 19 Menubar = Wx. menubar () 20 21 # File menu 22 Filemenu = Wx. menu () 23 Filemenu. append (-1, " & Open " , "" ) 24 Menubar. append (filemenu, " & File " ) 25 Self. BIND (wx. evt_menu, self. onfileopen) 26 27 # Edit menu 28 Editmenu = Wx. menu () 29 Editmenu. append (-1, " & Copy " , "" ) 30 Menubar. append (editmenu," & Edit " ) 31 32 # Help/about menu 33 Helpmenu = Wx. menu () 34 Helpmenu. append (-1, " About " , "" ) 35 Menubar. append (helpmenu, " & Help " ) 36 37 # Call setmenubar to display it in the Framework. 38 Self. setmenubar (menubar) 39 40 # Add a toolbar. Note: if you cannot create a toolbar with toolbar = wx. toolbar (), it will be replaced by other controls. Why? 41 # Toolbar = wx. toolbar (Self) 42 Toolbar = Self. createmedilbar () 43 Tsize = (24, 24 ) 44 New_bmp = Wx. artprovider. getbitmap (wx. art_new, wx. art_toolbar, tsize) 45 Toolbar. addsimpletool (-1, new_bmp, " Long help for 'new' " ) 46 Toolbar. Realize () 47 48 # Add Status Bar 49 Statusbar = Self. createstatusbar () 50 51 Def Onfileopen (self, event ): 52 # Create standard file dialog box 53 Dialog = wx. filedialog (self, " Open file... " , OS. getcwd (), style = wx. Open, wildcard = " *. Py " ) 54 # There is a concept: Modal Dialog Box and non-modal dialog box. The main difference is that the modal dialog box will block the response of other events, 55 # Other operations can be performed when the non-modal dialog box is displayed. The modal dialog box is displayed. The return values include wx. id_ OK and wx. id_canel; 56 If Dialog. showmodal () = Wx. id_ OK: 57 Self. filename = Dialog. getpath () 58 Self. onfileread () 59 # In topwindow, update the title as the file name. 60 Self. settitle (self. filename) 61 # In the destroy dialog box, release the resource. 62 Dialog. Destroy () 63 64 Def Onfileread (Self ): 65 If Self. filename: 66 Try : 67 FD = open (self. filename, ' R ' ) 68 Line = FD. Read () 69 # This is just to show the file content on the terminal. It can be displayed in the multi-line text control. This is the next step. 70 Print Line 71 FD. Close () 72 Except : 73 Wx. MessageBox ( " % S is not a match file. " % Self. filename, " Oops! " , Style = wx. OK | Wx. icon_exclamation) 74 75 If _ Name __ = " _ Main __ " : 76 APP = Wx. pysimpleapp () 77 Frame = Guimainframe () 78 Frame. Show () 79 App. mainloop ()