Python Forum
[WxPython] [Tutorial] Notespad - Create a text editor - W.I.P.
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[WxPython] [Tutorial] Notespad - Create a text editor - W.I.P.
#1
NOTE: This is still W.I.P.
In this tutorial, I'm going to attempt to build a notespad in WxPython in stages and describe what each part of the code is doing.
Updated to work with python 3 I am using python version 2.7.3, WxPython version 2.9.4.0, and  windowsXp.

WxPython site link
Links to existing WxPython tutorials that are probably way better then mine  :lol:
http://wiki.wxpython.org/Getting%20Started
http://zetcode.com/wxpython/
Lets get started.
import wx  # 1
if __name__ == '__main__':  # 2
    wx_app = wx.App(False)  # 3
    wx_app.MainLoop()  # 4

  1. Imports the wxPython module.
  2. This means only run the following code when you run this module directly, if you import this module, the following code will not run.
  3. Every Wxpython application needs one wx.App object, the False parameter means don't redirect stdout and stderr to a window.
  4. The method call MainLoop on the App object starts an infinite loop that checks for 'events', all the while there is a wx window in existence.
    we will get to events later, running this code as it is, will just exit the loop as there is no window created yet.
Lets create a Frame so we can actually see something happen, Running this code we should now actually see a very basic window, the mainloop is now infinitely looping, by clicking on the frames X an event will be sent to the App telling it that this window has been closed and as there are no other windows in existence will also exit the mainloop.
import wx
if __name__ == '__main__':
    wx_app = wx.App(False)
    frame = wx.Frame(None)  # 1
    frame.Show()  # 2
    wx_app.MainLoop()

  1. To start with we create a wx.Frame, a Frame is a container object that we can add other wx objects to, calling None tells the frame that it has no parent.
  2. To make the frame visible we have to call its Show method.
Lets make it a little more interesting, rather then just using the basic wx Frame we'll make our own class of Frame and call it Notepad.
import wx

class Notespad(wx.Frame):  # 1
    def __init__(self, *args, **kwargs):  # 2
        super(Notespad, self).__init__(*args, **kwargs)  # 3
        self.SetTitle('Untitled - Notespad')  # 4
        panel = wx.Panel(self)  # 5
        sizer = wx.BoxSizer(wx.VERTICAL)  # 6
        sizer.Add(panel, 1, wx.EXPAND)  # 7
        self.SetSizer(sizer)  # 8
        self.Layout()  # 9

if __name__ == '__main__':
    wx_app = wx.App(False)
    frame = Notespad(None)  # 10
    frame.Show()
    wx_app.MainLoop()

  1. Creates a new class named Notespad which inherits from a wxFrame.
  2. Allows us to pass in any attributes.
  3. The attributes we passed in are given to the wxFrame.
  4. Sets the title of the frame.
  5. Creates a wxPanel object, this is a container object that sits on the frame as a background, we tell it that the wxFrame is its parent by giving it the attribute self.
  6. Creates a sizer, this is a kind of invisible container that automatically arranges its objects on the sizer's owner.
  7. Adds the panel we created to the sizer, the second attribute number 1 is the proportion of the of the space in the sizer it will take, and as we set the sizer to be a vertical that's the direction the proportion relates to, for instance if we was to add another object to the sizer with the same proportion, both objects would take up half of the available space each in the vertical direction. The third attribute wxExpand tells the sizer to stretch the object to take up the full width in the opposite direction, the Horizontal in this case.
  8. the wxFrame is told to let the sizer we created, stretch into the available space it has.
  9. Calling this  tells any sizer to arrange the objects there are controlling the position and size of.
  10. Now we are creating an instance of our own wxFrame and again passing in the attribute None to indicate that it has no parent
Its not a very good Notespad yet as we cant even type any text into it, lets fix that.
import wx

class Notespad(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(Notespad, self).__init__(*args, **kwargs)
        self.SetTitle('Untitled - Notepad')
        panel = wx.Panel(self)
        txt_ctrl = wx.TextCtrl(panel, style=wx.TE_MULTILINE)  # 1
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(panel, 1, wx.EXPAND)
        p_sizer = wx.BoxSizer(wx.VERTICAL)  # 2
        p_sizer.Add(txt_ctrl, 1, wx.EXPAND)  # 3
        panel.SetSizer(p_sizer)  # 4
        self.SetSizer(sizer)
        self.Layout()

if __name__ == '__main__':
    wx_app = wx.App(False)
    frame = Notespad(None)
    frame.Show()
    wx_app.MainLoop()

  1. Creates a wxTextCtrl and set its parent as the panel the style parameter makes us able to type on more then just the first row
  2. Because the panel now has controls it needs a sizer to keep them under control just the the frame needed a sizer for the panel
  3. Adding the wxTextCtrl to the panels sizer with the same parameters used in the previous sizer's add method
  4. tells the panel to use this sizer
Reply


Messages In This Thread
[Tutorial] Notespad - Create a text editor - W.I.P. - by Yoriz - Sep-17-2016, 05:14 PM
RE: [WxPython][Tutorial] Notespad W.I.P. - by Yoriz - Sep-17-2016, 05:17 PM
RE: [Tutorial] Notespad W.I.P. - by Yoriz - Mar-27-2019, 10:23 PM
RE: [Tutorial] Notespad W.I.P. - by Yoriz - Mar-28-2019, 12:08 AM
RE: [Tutorial] Notespad W.I.P. - by Yoriz - Apr-30-2019, 10:30 PM
RE: [Tutorial] Notespad W.I.P. - by Yoriz - Apr-30-2019, 10:53 PM
RE: [Tutorial] Notespad W.I.P. - by Yoriz - May-03-2019, 10:59 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020