Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 12th, 2012  |  syntax: Python  |  size: 2.26 KB  |  views: 52  |  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. import wx
  2. import os
  3.  
  4. class MyFrame(wx.Frame):
  5.         def __init__(self, parent, title):
  6.                 wx.Frame.__init__(self, parent, title=title, size=(200, -1))
  7.                 self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
  8.                 self.dirname = ""
  9.                 self.filename = ""
  10.                 self.content = ""
  11.                 self.Show()
  12.  
  13.                 # Attribute
  14.                 statusBar = self.CreateStatusBar()
  15.                 fileMenu = wx.Menu()
  16.                 optMenu = wx.Menu()
  17.                 openf = fileMenu.Append(wx.ID_OPEN, "Open..", "Open a file")
  18.                 exit = fileMenu.Append(wx.ID_EXIT, "Exit", "Quit the program")
  19.                 about = optMenu.Append(wx.ID_ABOUT, "About", "About this software")
  20.  
  21.                 # Assign menus to menubar and activate menu bar
  22.                 menuBar = wx.MenuBar()
  23.                 menuBar.Append(fileMenu, "File")
  24.                 menuBar.Append(optMenu, "Option")
  25.                 self.SetMenuBar(menuBar)
  26.                
  27.                 # Bind EVT to the menu choices
  28.                 self.Bind(wx.EVT_MENU, self.OnOpen, openf)
  29.                 self.Bind(wx.EVT_MENU, self.OnExit, exit)
  30.                 self.Bind(wx.EVT_MENU, self.OnAbout, about)
  31.  
  32.                 # Setup BoxSizer
  33.                 self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
  34.                 self.buttons = []
  35.                 for i in range(0,6):
  36.                         self.buttons.append(wx.Button(self, -1, "Button &"+str(i)))
  37.                         self.sizer2.Add(self.button[i], 1, wx.EXPAND)
  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.                 # Layout sizers
  43.                 self.SetSizer(self.sizer)
  44.                 self.SetAutoLayout(1)
  45.                 self.sizer.Fit(self)
  46.                 #self.Show()
  47.  
  48.         def OnOpen(self, event):
  49.                 self.content = ""
  50.                 opendlg = wx.FileDialog(self, "Open a file..", self.dirname, "", "*.*", wx.OPEN)
  51.                 if opendlg.ShowModal() == wx.ID_OK:
  52.                         self.dirname = opendlg.GetDirectory()
  53.                         self.filename = opendlg.GetFilename()
  54.                         f = open(os.path.join(self.dirname, self.filename), "r")
  55.                         self.content = f.read()
  56.                         self.control.SetValue(self.content)     # set TextCtrl value
  57.                         f.close()
  58.                 opendlg.Destroy()
  59.  
  60.         def OnExit(self, event):
  61.                 if self.control.GetValue() != self.content:
  62.                         self.control.SaveFile(os.path.join(self.dirname, self.filename))
  63.                 self.Destroy()
  64.  
  65.         def OnAbout(self, event):
  66.                 aboutdlg = wx.MessageDialog(self, "This is a free dummy editor.", "About editor", wx.OK)
  67.                 aboutdlg.ShowModal()
  68.                 aboutdlg.Destroy()
  69.        
  70. app = wx.App(True)
  71. frame = MyFrame(None, title="Editor")
  72. app.MainLoop()