a simple notepad in wxPython

Reply

Join Date: Mar 2006
Posts: 18
Reputation: sharma_vivek82 is an unknown quantity at this point 
Solved Threads: 0
sharma_vivek82 sharma_vivek82 is offline Offline
Newbie Poster

a simple notepad in wxPython

 
0
  #1
Jan 17th, 2007
code:
you can change this code as you like

  1. import wx
  2. import os
  3. from wxPython.wx import *
  4. import wx.html
  5.  
  6. class MyHtmlFrame(wx.Frame):
  7.  
  8. def __init__(self, parent, title):
  9.  
  10. wx.Frame.__init__(self, parent, -1, title)
  11.  
  12. html = wx.html.HtmlWindow(self)
  13. html.SetPage(
  14. "This is test implementation of Simple <b>Notepad</b> using <font color=\"red\">wxPython</font> !!!! "
  15. "<br>View is created using <font color=\"red\">wxGlade</font>")
  16.  
  17. class MyFrame(wx.Frame):
  18. def __init__(self, *args, **kwds):
  19. # begin wxGlade: MyFrame.__init__
  20. kwds["style"] = wx.DEFAULT_FRAME_STYLE
  21. wx.Frame.__init__(self, *args, **kwds)
  22. #wx.Frame.__init__(self, parent, -1, title, pos=(150, 150), size=(350, 250))
  23.  
  24. self.flag = 0
  25. # Menu Bar
  26. self.frame_1_menubar = wx.MenuBar()
  27.  
  28. self.SetMenuBar(self.frame_1_menubar)
  29. self.File = wx.Menu()
  30. self.New = wx.MenuItem(self.File, wx.NewId(), "&New", "", wx.ITEM_NORMAL)
  31. self.File.AppendItem(self.New)
  32. self.Save = wx.MenuItem(self.File, wx.NewId(), "Save &As", "", wx.ITEM_NORMAL)
  33. self.File.AppendItem(self.Save)
  34. self.open = wx.MenuItem(self.File, wx.NewId(), "&Open", "", wx.ITEM_NORMAL)
  35. self.File.AppendItem(self.open)
  36. self.About = wx.MenuItem(self.File, wx.NewId(), "A&bout", "", wx.ITEM_NORMAL)
  37. self.File.AppendItem(self.About)
  38. self.Exit = wx.MenuItem(self.File, wx.NewId(), "&Exit", "", wx.ITEM_NORMAL)
  39. self.File.AppendItem(self.Exit)
  40. self.frame_1_menubar.Append(self.File, "&File")
  41. # Menu Bar end
  42.  
  43. # Tool Bar
  44.  
  45. #self.frame_1_toolbar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL|wx.TB_FLAT|wx.TB_DOCKABLE|wx.TB_3DBUTTONS|wx.TB_TEXT|wx.TB_NOICONS|wx.TB_NODIVIDER|wx.TB_NOALIGN|wx.TB_HORZ_LAYOUT|wx.TB_HORZ_TEXT)
  46. #self.SetToolBar(self.frame_1_toolbar)
  47.  
  48. # Tool Bar end
  49.  
  50. self.frame_1_statusbar = self.CreateStatusBar(1, 0)
  51.  
  52.  
  53. self.__set_properties()
  54. self.__do_layout()
  55. self.SetStatusText("Notepad using wxPython")
  56. self.Bind(wx.EVT_MENU, self.file_save, self.Save)
  57. self.Bind(wx.EVT_MENU, self.open_file, self.open)
  58. self.Bind(wx.EVT_MENU, self.file_new, self.New)
  59. self.Bind(wx.EVT_MENU, self.file_exit, self.Exit)
  60. self.Bind(wx.EVT_MENU, self.about, self.About)
  61.  
  62. # end wxGlade
  63.  
  64. def __set_properties(self):
  65. # begin wxGlade: MyFrame.__set_properties
  66. self.SetTitle("Simple Notepad using wxPython")
  67. #self.frame_1_toolbar.SetToolBitmapSize((0, 0))
  68. #self.frame_1_toolbar.Realize()
  69. self.frame_1_statusbar.SetStatusWidths([-1])
  70. # statusbar fields
  71. frame_1_statusbar_fields = ["frame_1_statusbar"]
  72. for i in range(len(frame_1_statusbar_fields)):
  73. self.frame_1_statusbar.SetStatusText(frame_1_statusbar_fields[i], i)
  74. # end wxGlade
  75.  
  76. def __do_layout(self):
  77. # begin wxGlade: MyFrame.__do_layout
  78. sizer_1 = wx.BoxSizer(wx.VERTICAL)
  79. self.SetAutoLayout(True)
  80. self.SetSizer(sizer_1)
  81. sizer_1.Fit(self)
  82. sizer_1.SetSizeHints(self)
  83. self.Layout()
  84. # end wxGlade
  85.  
  86. def file_save(self, event): # wxGlade: MyFrame.<event_handler>
  87. if self.flag == 1 :
  88. dialog = wxFileDialog ( None, style = wxSAVE )
  89. # Show the dialog and get user input
  90. if dialog.ShowModal() == wxID_OK:
  91. file_path = dialog.GetPath()
  92. file = open(file_path,'w')
  93. file_content = self.text_ctrl_1.GetValue()
  94. file.write(file_content)
  95. else:
  96. print 'Nothing was selected.'
  97. # Destroy the dialog
  98. self.SetStatusText("Your file has been saved")
  99. dialog.Destroy()
  100. else:
  101. dlg = wxMessageDialog(self, "plz open a new file !!!!",
  102. "New File", wxOK | wxICON_INFORMATION)
  103. dlg.ShowModal()
  104. dlg.Destroy()
  105.  
  106. def open_file(self, event): # wxGlade: MyFrame.<event_handler>
  107. if self.flag ==1:
  108. filters = 'Text files (*.txt)|*.txt'
  109. dialog = wxFileDialog ( None, message = 'Open something....', wildcard = filters, style = wxOPEN | wxMULTIPLE )
  110. if dialog.ShowModal() == wxID_OK:
  111. filename = dialog.GetPath()
  112. file = open(filename,'r')
  113. file_content = file.read()
  114. self.text_ctrl_1.SetValue(file_content)
  115. else:
  116. print 'Nothing was selected.'
  117. dialog.Destroy()
  118. else:
  119. dlg = wxMessageDialog(self, "plz open a new file !!!!",
  120. "New File", wxOK | wxICON_INFORMATION)
  121. dlg.ShowModal()
  122.  
  123. # Destroy the dialog
  124. self.SetStatusText("Your file is opened")
  125. dlg.Destroy()
  126.  
  127. def file_new(self, event): # wxGlade: MyFrame.<event_handler>
  128. self.flag = 1
  129. sizer_1 = wx.BoxSizer(wx.VERTICAL)
  130. self.text_ctrl_1 = wx.TextCtrl(self, wx.ID_ANY, style=wx.TE_MULTILINE| wx.SUNKEN_BORDER)
  131. sizer_1.Add(self.text_ctrl_1, 0, wx.ALL|wx.EXPAND|wx.ALIGN_RIGHT|wx.ADJUST_MINSIZE, 0)
  132. self.text_ctrl_1.SetMinSize((500, 500))
  133. self.SetAutoLayout(True)
  134. self.SetSizer(sizer_1)
  135. sizer_1.Fit(self)
  136. sizer_1.SetSizeHints(self)
  137. self.Layout()
  138. self.text_ctrl_1.SetFocus()
  139.  
  140. def file_exit(self, event): # wxGlade: MyFrame.<event_handler>
  141. """Bring up a wx.MessageDialog with a quit message."""
  142. alert = wx.MessageDialog(self, "Do you really want to quit")
  143. response = alert.ShowModal()
  144. alert.Destroy()
  145.  
  146. if response == wx.ID_OK:
  147. print "The user clicked the 'OK' button."
  148. self.Close()
  149. else:
  150. print "The user clicked the 'Cancel' button."
  151. event.Skip()
  152. def about(self,event):
  153. frm = MyHtmlFrame(None, "About ....")
  154. frm.Show()
  155.  
  156. #dlg = wxMessageDialog(self, "This is test implementation of Simple Notepad using wxPython !!!!",
  157. # "New File", wxOK | wxICON_INFORMATION)
  158. #dlg.ShowModal()
  159. #dlg.Destroy()
  160.  
  161. # end of class MyFrame
  162.  
  163.  
  164. class MyNotepad(wx.App):
  165. def OnInit(self):
  166. wx.InitAllImageHandlers()
  167. frame_1 = MyFrame(None, -1,"Notepad")
  168. self.SetTopWindow(frame_1)
  169. frame_1.Show()
  170. return 1
  171.  
  172. # end of class MyNotepad
  173.  
  174. if __name__ == "__main__":
  175. app = MyNotepad(0)
  176. app.MainLoop()
Last edited by stymiee; Jan 17th, 2007 at 1:34 pm. Reason: added code tags
Best wishes,
Vivek Sharma
Zope/Python Developer
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: a simple notepad in wxPython

 
0
  #2
Jan 18th, 2007
What is wxGlade?
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 521
Reputation: pty is on a distinguished road 
Solved Threads: 37
pty's Avatar
pty pty is offline Offline
Posting Pro

Re: a simple notepad in wxPython

 
0
  #3
Jan 19th, 2007
Originally Posted by sneekula View Post
What is wxGlade?
wxGlade is a interface builder for wxWidgets.

They copied the look/layout from Gnome's Glade (which designs GTK GUIs)
Note to self... pocket cup
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC