Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Mar 12th, 2012  |  syntax: Python  |  size: 2.62 KB  |  hits: 35  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # TUTORIAL WORKING CODE
  2.  
  3. import wx
  4. import os
  5.  
  6. class MainWindow(wx.Frame):
  7.     def __init__(self, parent, title):
  8.         self.dirname=''
  9.  
  10.         # A "-1" in the size parameter instructs wxWidgets to use the default size.
  11.         # In this case, we select 200px width and the default height.
  12.         wx.Frame.__init__(self, parent, title=title, size=(200,-1))
  13.         self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
  14.         self.CreateStatusBar() # A Statusbar in the bottom of the window
  15.  
  16.         # Setting up the menu.
  17.         filemenu= wx.Menu()
  18.         menuOpen = filemenu.Append(wx.ID_OPEN, "&Open"," Open a file to edit")
  19.         menuAbout= filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
  20.         menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")
  21.  
  22.         # Creating the menubar.
  23.         menuBar = wx.MenuBar()
  24.         menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
  25.         self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
  26.  
  27.         # Events.
  28.         self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
  29.         self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
  30.         self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
  31.  
  32.         self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
  33.         self.buttons = []
  34.         for i in range(0, 6):
  35.             self.buttons.append(wx.Button(self, -1, "Button &"+str(i)))
  36.             self.sizer2.Add(self.buttons[i], 1, wx.EXPAND)
  37.  
  38.         # Use some sizers to see layout options
  39.         self.sizer = wx.BoxSizer(wx.VERTICAL)
  40.         self.sizer.Add(self.control, 1, wx.EXPAND)
  41.         self.sizer.Add(self.sizer2, 0, wx.EXPAND)
  42.  
  43.         #Layout sizers
  44.         self.SetSizer(self.sizer)
  45.         self.SetAutoLayout(1)
  46.         self.sizer.Fit(self)
  47.         self.Show()
  48.  
  49.     def OnAbout(self,e):
  50.         # Create a message dialog box
  51.         dlg = wx.MessageDialog(self, " A sample editor \n in wxPython", "About Sample Editor", wx.OK)
  52.         dlg.ShowModal() # Shows it
  53.         dlg.Destroy() # finally destroy it when finished.
  54.  
  55.     def OnExit(self,e):
  56.         self.Close(True)  # Close the frame.
  57.  
  58.     def OnOpen(self,e):
  59.         """ Open a file"""
  60.         dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
  61.         if dlg.ShowModal() == wx.ID_OK:
  62.             self.filename = dlg.GetFilename()
  63.             self.dirname = dlg.GetDirectory()
  64.             f = open(os.path.join(self.dirname, self.filename), 'r')
  65.             self.control.SetValue(f.read())
  66.             f.close()
  67.         dlg.Destroy()
  68.  
  69. app = wx.App(False)
  70. frame = MainWindow(None, "Sample editor")
  71. app.MainLoop()