Old code / broken code

I’ve found a nice example here .

The problem is that I can’t get it to work - most likely wx syntax that has changed in the last five years. I am interested in writing a program based on this example, so I would like to learn from it (rather than messing around first with controls I don’t need in my app). I am new to wxPython.

I have emailed the author, but the article is 5 years old…so I’m not holding my breath…

Can any of you guys help me fix it?

Kind regards
Derick

Yes you are right, the page is very old (and uses the old namespace).

Look at the wxPython Demo

(In my case):
C:\Programme\wxPython2.6 Docs and Demos\samples\wxProject\wxProject.py

But nevertheless, also here I saw some tracebacks.
This sample could be checked through again.

···

On Wed, 8 Feb 2006 10:24:06 +0200, Derick Van Niekerk <derickvn@gmail.com> wrote:

I've found a nice example
here<http://www-128.ibm.com/developerworks/library/l-wxpy/listing2.html&gt;\.

The problem is that I can't get it to work - most likely wx syntax that has
changed in the last five years. I am interested in writing a program based
on this example, so I would like to learn from it (rather than messing
around first with controls I don't need in my app). I am new to wxPython.

I have emailed the author, but the article is 5 years old...so I'm not
holding my breath...

Can any of you guys help me fix it?

Kind regards
Derick
*

--
Franz Steinhaeusler

The demo seems to work fine - Thanks!

I wonder why it wasn’t updated on the site…or why the tutorial isn’t included with the code? You are right about the tracebacks, it looks like the implementation of the TreeCtl might have changed. Oh well, at least now I have (semi)working source to play with :slight_smile:

Yes you are right, the page is very old (and uses the old namespace).

More like no namespace at all…

Derick

···

Look at the wxPython Demo

(In my case):
C:\Programme\wxPython2.6 Docs and Demos\samples\wxProject\wxProject.py

But nevertheless, also here I saw some tracebacks.
This sample could be checked through again.

Franz Steinhaeusler


To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org

For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Hello Derick,
My advice is to start here:
http://wiki.wxpython.org/index.cgi/Getting_20Started#head-f423bae42bbd4b4495432886d58c04bc99dd58da
It is a better place to start learning :slight_smile:

Peter.

···

On 2/8/06, Derick Van Niekerk <derickvn@gmail.com> wrote:

I've found a nice example here .

The problem is that I can't get it to work - most likely wx syntax that has
changed in the last five years. I am interested in writing a program based
on this example, so I would like to learn from it (rather than messing
around first with controls I don't need in my app). I am new to wxPython.

I have emailed the author, but the article is 5 years old...so I'm not
holding my breath...

Can any of you guys help me fix it?

Kind regards
Derick

Yep - still working through it. It’s a very good resource, but I want to hit the ground running - I’ve found the best way to learn a new language / library is to actually use it. Especially if, like me, you already have a specific task in mind…

Derick

···

On 2/8/06, Peter Damoc pdamoc@gmail.com wrote:

Hello Derick,
My advice is to start here:
http://wiki.wxpython.org/index.cgi/Getting_20Started#head-f423bae42bbd4b4495432886d58c04bc99dd58da

It is a better place to start learning :slight_smile:

Peter.

Derick,
That's great...
If you want an extra challenge... take a look here:
http://wiki.wxpython.org/index.cgi/ModelViewPresenter
Peter.

···

On 2/8/06, Derick Van Niekerk <derickvn@gmail.com> wrote:

Yep - still working through it. It's a very good resource, but I want to hit
the ground running - I've found the best way to learn a new language /
library is to actually use it. Especially if, like me, you already have a
specific task in mind...

Derick

On 2/8/06, Peter Damoc <pdamoc@gmail.com> wrote:
> Hello Derick,
> My advice is to start here:
>
http://wiki.wxpython.org/index.cgi/Getting_20Started#head-f423bae42bbd4b4495432886d58c04bc99dd58da
> It is a better place to start learning :slight_smile:
>
> Peter.
>

Thanks - bookmarked for later. A bit over my head at the moment, but definitely worth looking into.

Something a bit more bite-sized if there is someone else out there on my level - A good introductory tutorial:

Write it yourself tutorial

Derick

···

On 2/8/06, Peter Damoc < pdamoc@gmail.com> wrote:

Derick,
That’s great…
If you want an extra challenge… take a look here:

http://wiki.wxpython.org/index.cgi/ModelViewPresenter
Peter.

you could take a look at an old attempt to rewrite the Getting Started guide:
http://wiki.wxpython.org/index.cgi/GettingStarted

And one more thing... there is this thing called *fear* that will
hinder you, especialy now, when you feel you are a begginer. Don't
worry, the feeling will dissapear, the sooner the better... Use
courage to fight it... or use something like this:

LITANY AGAINST FEAR
I must not fear.
Fear is the mind-killer.
Fear is the little-death that brings total obliteration.
I will face my fear.
I will permit it to pass over me and through me.
And when it has gone past I will turn the inner eye to see its path.
Where the fear has gone there will be nothing.
Only I will remain.

Stuff like this will allow you to overcome the initial fear and
actualy "see" the code. I believe in you... because I was you... Look,
as a courage exercise tell yourself this:
"I will read the entire page, I will download and look at the source
code provided in that page and when I finish I will write on a piece
of paper what I understood and what I failed to understand".

try courage for a change... it feels better :wink:

Peter.

···

On 2/8/06, Derick Van Niekerk <derickvn@gmail.com> wrote:

Thanks - bookmarked for later. A bit over my head at the moment, but
definitely worth looking into.

Something a bit more bite-sized if there is someone else out there on my
level - A good introductory tutorial:

Write it yourself tutorial

Derick

On 2/8/06, Peter Damoc < pdamoc@gmail.com> wrote:
> Derick,
> That's great...
> If you want an extra challenge... take a look here:
> http://wiki.wxpython.org/index.cgi/ModelViewPresenter
> Peter.
>

So I have updated wxProject.py

maybe it is getting (so or with some changes) into the next wxPython Distribution.

If the comments are not ok (I'm no native english speaker),
please feel free to correct them.

···

On Wed, 8 Feb 2006 11:13:27 +0200, Derick Van Niekerk <derickvn@gmail.com> wrote:

The demo seems to work fine - Thanks!

I wonder why it wasn't updated on the site...or why the tutorial isn't
included with the code? You are right about the tracebacks, it looks like
the implementation of the TreeCtl might have changed. Oh well, at least now
I have (semi)working source to play with :slight_smile:

Yes you are right, the page is very old (and uses the old namespace).

============================================================================================

#!/bin/python

# This sample comes from an IBM developerWorks article at
# IBM Developer

# Modified by Franz Steinhaeusler 08.Feb.2006.
# tried to meet the wxPython Style Guide
# (http://wiki.wxpython.org/index.cgi/wxPython_20Style_20Guide)
# (also take care to use id's as less as possible
# added docstrings
# used wx Namespace (after wxPython 2.5) and Bind instead directly EVT_MENU
# set indentation to 4 spaces
# used MsgDlg for better overview
# added info (for adding or removing files without a root item)
# completed the menu function: File open
# if a file was changed, and you want to quit the app, you are now
# asked to save the file.
# there was a traceback with self.tree.GetFirstChild(self.root). fixed.
# close handler (save file, if you also close with mouse or alt-F4)

import sys, os
import wx
from string import *

# Process the command line. Not much to do;
# just get the name of the project file if it's given. Simple.
projfile = 'Unnamed'
if len(sys.argv) > 1:
    projfile = sys.argv[1]

def MsgDlg (window, string, caption='wxProject', style=wx.YES_NO|wx.CANCEL):
    """Common MessageDialog."""
    dlg = wx.MessageDialog(window, string, caption, style)
    result = dlg.ShowModal()
    dlg.Destroy()
    return result

class main_window(wx.Frame):
    """wxProject MainFrame."""
    def __init__(self, parent, title):
        """Create the wxProject MainFrame."""
        wx.Frame.__init__(self, parent, title=title, size=(500, 500),
                          style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)

        # Set up menu bar for the program.
        self.mainmenu = wx.MenuBar() # Create menu bar.
        mainwindow = self

        menu = wx.Menu() # Make a menu (will be the Project menu)
        
        nId = wx.NewId() # Make a new ID for every menu entry.
        menu.Append(nId, '&Open', 'Open project') # Append a new menu
        self.Bind(wx.EVT_MENU, self.OnProjectOpen, id=nId) # Create and assign a menu event.
        nId = wx.NewId()
        menu.Append(nId, '&New', 'New project')
        self.Bind(wx.EVT_MENU, self.OnProjectNew, id=nId, )
        nId = wx.NewId()
        menu.Append(nId, 'E&xit', 'Exit program')
        self.Bind(wx.EVT_MENU, self.OnProjectExit, id=nId)
        self.Bind(wx.EVT_CLOSE, self.OnProjectExit)
        self.mainmenu.Append (menu, '&Project') # Add the project menu to the menu bar.

        menu = wx.Menu() # Make a menu (will be the File menu)

        nId = wx.NewId()
        menu.Append(nId, '&Add', 'Add file to project')
        self.Bind(wx.EVT_MENU, self.OnFileAdd, id=nId)
        nId = wx.NewId()
        menu.Append(nId, '&Remove', 'Remove file from project')
        self.Bind(wx.EVT_MENU, self.OnFileRemove, id=nId)
        nId = wx.NewId()
        menu.Append(nId, '&Open', 'Open file for editing')
        self.Bind(wx.EVT_MENU, self.OnFileOpen, id=nId)
        nId = wx.NewId()
        menu.Append(nId, '&Save', 'Save file')
        self.Bind(wx.EVT_MENU, self.OnFileSave, id=nId)

        self.mainmenu.Append (menu, '&File') # Add the file menu to the menu bar.

        self.SetMenuBar (self.mainmenu) # Attach the menu bar to the window.

        # Create the splitter window.
        splitter = wx.SplitterWindow(self, style=wx.NO_3D|wx.SP_3D)
        splitter.SetMinimumPaneSize(1)

        # Create the tree on the left.
        tID = wx.NewId()
        self.tree = wx.TreeCtrl(splitter, tID, style=wx.TR_HAS_BUTTONS|
                                   wx.TR_EDIT_LABELS|
                                   wx.TR_HAS_VARIABLE_ROW_HEIGHT)
        self.tree.Bind(wx.EVT_TREE_BEGIN_LABEL_EDIT, self.OnTreeLabelEdit, id=tID)
        self.tree.Bind(wx.EVT_TREE_END_LABEL_EDIT, self.OnTreeLabelEditEnd, id=tID)
        self.tree.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnTreeItemActivated, id=tID)

        # Create the editor on the right.
        self.editor = wx.TextCtrl(splitter, style=wx.TE_MULTILINE)
        self.editor.Enable (0)

        # Install the tree and the editor.
        splitter.SplitVertically (self.tree, self.editor)
        splitter.SetSashPosition (180, True)

        self.Show(True)

        # Some global state variables.
        self.projectdirty = False
        self.root = None
        self.close = False

    # ----------------------------------------------------------------------------------------
    # Some nice little handlers.
    # ----------------------------------------------------------------------------------------

    def project_open(self, project_file):
        """Open and process a wxProject file."""
        try:
            input = open (project_file, 'r')
            self.tree.DeleteAllItems()

            self.project_file = project_file
            name = replace (input.readline(), "\n", "")
            self.SetTitle (name)
            self.root = self.tree.AddRoot(name)
            self.activeitem = self.root
            for line in input.readlines():
                self.tree.AppendItem(self.root, replace(line, "\n", ""))
            input.close
            self.tree.Expand(self.root)

            self.editor.Clear()
            self.editor.Enable(False)

            self.projectdirty = False
        except IOError:
            pass

    def project_save(self):
        """Save a wxProject file."""
        try:
            output = open (self.project_file, 'w+')
            output.write (self.tree.GetItemText (self.root) + "\n")
            count = self.tree.GetChildrenCount (self.root)
            iter = 0
            child = ''
            for i in range(count):
               if i == 0:
                  child, cookie = self.tree.GetFirstChild(self.root)
               else:
                  child, iter = self.tree.GetNextChild(self.root, cookie)
               output.write (self.tree.GetItemText(child) + "\n")
            output.close()
            self.projectdirty = False
        except IOError:
            MsgDlg(self, 'There was an error saving the new project file.', 'Error!', wx.OK)

    # ----------------------------------------------------------------------------------------
    # Event handlers from here on out.
    # ----------------------------------------------------------------------------------------

    def OnProjectOpen(self, event):
        """Open a wxProject file."""
        open_it = True
        if self.projectdirty:
            result = MsgDlg(self, 'The project has been changed. Save?')
            if result == wx.ID_YES:
                self.project_save()
            if result == wx.ID_CANCEL:
                open_it = False
        if open_it:
            dlg = wx.FileDialog(self, "Choose a project to open", ".", "", "*.wxp", wx.OPEN)
            if dlg.ShowModal() == wx.ID_OK:
                self.project_open(dlg.GetPath())
            dlg.Destroy()

    def OnProjectNew(self, event):
        """Create a new wxProject."""
        open_it = True
        if self.projectdirty:
            result = MsgDlg(self, 'The project has been changed. Save?')
            if result == wx.ID_YES:
                self.project_save()
            if result == wx.ID_CANCEL:
                open_it = False

        if open_it:
            dlg = wx.TextEntryDialog (self, "Name for new project:", "New Project",
                                    "New project", wx.OK | wx.CANCEL)
            if dlg.ShowModal() == wx.ID_OK:
                newproj = dlg.GetValue()
                dlg.Destroy()
                dlg = wx.FileDialog (self, "Place to store new project", ".", "", "*.wxp",
                                   wx.SAVE)
                if dlg.ShowModal() == wx.ID_OK:
                    try:
                        proj = open (dlg.GetPath(), 'w')
                        proj.write (newproj + "\n")
                        proj.close()
                        self.project_open (dlg.GetPath())
                    except IOError:
                        MsgDlg(self, 'There was an error saving the new project file.', 'Error!', wx.OK)
            dlg.Destroy()

    def CheckRoot(self):
        """Is there any root item?"""
        if not self.root:
            MsgDlg(self, 'Please create or open a project before', 'Error!', wx.OK)
            return False
        return True

    def SaveCurrentFile(self):
        """Check and save current file."""
        go_ahead = True
        if self.root:
            if self.activeitem != self.root:
                if self.editor.IsModified(): # Save modified file before
                    result = MsgDlg(self, 'The edited file has changed. Save it?')
                    if result == wx.ID_YES:
                        self.editor.SaveFile (self.tree.GetItemText (self.activeitem))
                    if result == wx.ID_CANCEL:
                        go_ahead = False
                if go_ahead:
                    self.tree.SetItemBold (self.activeitem, 0)
        return go_ahead
    
    def OnProjectExit(self, event):
        """Quit the program."""
        if not self.close:
            self.close = True
            if not self.SaveCurrentFile():
                self.close = False
            if self.projectdirty and self.close:
                result = MsgDlg(self, 'The project has been changed. Save?')
                if result == wx.ID_YES:
                    self.project_save()
                if result == wx.ID_CANCEL:
                    self.close = False
            if self.close:
                self.Close()
        else:
            event.Skip()

    def OnFileAdd(self, event):
        """Adds a file to the current project."""
        if not self.CheckRoot():
            return
          
        dlg = wx.FileDialog (self, "Choose a file to add", ".", "", "*.*", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            path = os.path.split(dlg.GetPath())
            self.tree.AppendItem (self.root, path[1])
            self.tree.Expand (self.root)
            self.project_save()

    def OnFileRemove(self, event):
        """Removes a file to the current project."""
        if not self.CheckRoot():
            return
        item = self.tree.GetSelection()
        if item != self.root:
            self.tree.Delete (item)
            self.project_save()

    def OnFileOpen(self, event):
        """Opens current selected file."""
        item = self.tree.GetSelection()
        self.OnTreeItemActivated(None, item)

    def OnFileSave(self, event):
        """Saves current selected file."""
        if self.activeitem != self.root:
            self.editor.SaveFile (self.tree.GetItemText (self.activeitem))

    def OnTreeLabelEdit(self, event):
        """Edit tree label (only root label can be edited)."""
        item = event.GetItem()
        if item != self.root:
            event.Veto()

    def OnTreeLabelEditEnd(self, event):
        """End editing the tree label."""
        self.projectdirty = True

    def OnTreeItemActivated(self, event, item=None):
        """Tree item was activated: try to open this file."""
        go_ahead = self.SaveCurrentFile()

        if go_ahead:
            if event:
                item = event.GetItem()
            self.activeitem = item
            if item != self.root:
                self.tree.SetItemBold (item, 1)
                self.editor.Enable (1)
                self.editor.LoadFile (self.tree.GetItemText(item))
                self.editor.SetInsertionPoint (0)
                self.editor.SetFocus()
            else:
                self.editor.Clear()
                self.editor.Enable (0)

class App(wx.App):
    """wxProject Application."""
    def OnInit(self):
        """Create the wxProject Application."""
        frame = main_window(None, "wxProject - " + projfile)
        self.SetTopWindow(frame)
        if (projfile != 'Unnamed'):
            frame.project_open(projfile)
        return True

if __name__ == "__main__":
    app = App(0)
    app.MainLoop()

============================================================================================

--
Franz Steinhaeusler

Pretty intense… :confused:

Got the coffee, the code printouts and tutorials - now I’m well armed.
The code will submit to me now!

Derick

···

And one more thing… there is this thing called fear that will
hinder you, especialy now, when you feel you are a begginer. Don’t
worry, the feeling will dissapear, the sooner the better… Use
courage to fight it… or use something like this:

LITANY AGAINST FEAR
I must not fear.
Fear is the mind-killer.
Fear is the little-death that brings total obliteration.
I will face my fear.
I will permit it to pass over me and through me.
And when it has gone past I will turn the inner eye to see its path.

Where the fear has gone there will be nothing.
Only I will remain.

Stuff like this will allow you to overcome the initial fear and
actualy “see” the code. I believe in you… because I was you… Look,

as a courage exercise tell yourself this:
“I will read the entire page, I will download and look at the source
code provided in that page and when I finish I will write on a piece
of paper what I understood and what I failed to understand”.

try courage for a change… it feels better :wink:

Peter.

There were still some small errors.
the last one is on:
http://mitglied.lycos.de/drpython/wxProject.py

···

On Wed, 08 Feb 2006 14:06:06 +0100, Franz Steinhaeusler <franz.steinhaeusler@gmx.at> wrote:

On Wed, 8 Feb 2006 11:13:27 +0200, Derick Van Niekerk <derickvn@gmail.com> wrote:

Yes you are right, the page is very old (and uses the old namespace).

So I have updated wxProject.py

--
Franz Steinhaeusler

Franz Steinhaeusler wrote:

So I have updated wxProject.py

maybe it is getting (so or with some changes) into the next wxPython Distribution.

Suggestion: put it in the wxWiki, maybe link to it from a new section on the front page called "Example Apps".

Don Dwiggins
Advanced Publishing Technology

Franz Steinhaeusler wrote:

So I have updated wxProject.py

There were still some small errors.
the last one is on:
http://mitglied.lycos.de/drpython/wxProject.py

Franz, nice job. I've edited it a bit more, to conform to:

http://wiki.wxpython.org/index.cgi/wxPython_20Style_20Guide

Mostly I removed a bunch of superfluous IDs.

I've enclosed my version.

-Chris

wxProject.py (11.1 KB)

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Thank you Chris (for your interest and also for your corrections).

I've made also some changes (noted in the readme.txt).

I just uploaded it to wxProject090206.zip.

(I will also add an entry in the wxPyhton Patch section.)

···

On Wed, 08 Feb 2006 14:34:45 -0800, Christopher Barker <Chris.Barker@noaa.gov> wrote:

Franz Steinhaeusler wrote:

So I have updated wxProject.py

There were still some small errors.
the last one is on:
http://mitglied.lycos.de/drpython/wxProject.py

Franz, nice job. I've edited it a bit more, to conform to:

http://wiki.wxpython.org/index.cgi/wxPython_20Style_20Guide

Mostly I removed a bunch of superfluous IDs.

I've enclosed my version.

-Chris

--
Franz Steinhaeusler

I meant of course:

http://mitglied.lycos.de/drpython/wxProject090206.zip

···

On Thu, 09 Feb 2006 10:21:08 +0100, Franz Steinhaeusler <franz.steinhaeusler@gmx.at> wrote:

I just uploaded it to wxProject090206.zip.

--
Franz Steinhaeusler

Hello Don,

good idea.

I want to wait, until the code is "commonly accepted". :slight_smile:

···

On Wed, 08 Feb 2006 10:14:25 -0800, Don Dwiggins <ddwiggins@advpubtech.com> wrote:

Franz Steinhaeusler wrote:

So I have updated wxProject.py

maybe it is getting (so or with some changes) into the next wxPython Distribution.

Suggestion: put it in the wxWiki, maybe link to it from a new section on
the front page called "Example Apps".

Don Dwiggins
Advanced Publishing Technology

--
Franz Steinhaeusler

Franz Steinhaeusler wrote:

I want to wait, until the code is "commonly accepted". :slight_smile:

It's in good shape now. Better than much of what is in the Wiki (including stuff I put in there). Besides, the point of a Wiki is that it can easily be changed and commented on.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

ok.

Added in

http://wiki.wxpython.org/index.cgi/Getting_20Started
15.4 A rudimentary project organizer - Complete Source

there is a link to:
http://wiki.wxpython.org/index.cgi/WxProject

Please feel free to further edit this pages.

BTW:

is there any volunteer, who wants to
following files
http://wiki.wxpython.org/index.cgi/WxHowtoSmallEditor
http://wiki.wxpython.org/index.cgi/WxHowtoBuildingForms
http://wiki.wxpython.org/index.cgi/WxHowtoDrawing

to the wx Namespace and Bind functions?

···

On Thu, 09 Feb 2006 15:04:41 -0800, Christopher Barker <Chris.Barker@noaa.gov> wrote:

Franz Steinhaeusler wrote:

I want to wait, until the code is "commonly accepted". :slight_smile:

It's in good shape now. Better than much of what is in the Wiki
(including stuff I put in there). Besides, the point of a Wiki is that
it can easily be changed and commented on.

-Chris

--
Franz Steinhaeusler

Franz Steinhaeusler schreef:

is there any volunteer, who wants to
following files
http://wiki.wxpython.org/index.cgi/WxHowtoSmallEditor

I updated the one above. Any comments?

Cheers, Frank

Hello Frank,

I copy/pasted the sample into my editor.

My annotations:

1) I got tab indentation (The python styling says, spaces with
  indenation 4 should be used).

2) What I don't understand:
    super(MainWindow, self).SetTitle('Editor %s'%self.filename)

   Why this circumstantial super method?
   I would prefer:
   wx.Frame.SetTitle (self, 'Editor %s'%self.filename)
   It looks simpler and more clearly-presented

Else it looks nice and you used some not so often
seen concepts (the **thing and the fashion you create
the menus looks also interesting).

BTW: some docstrings more (but this could be a matter of taste of
  course).

Cheers,

···

On Sun, 12 Feb 2006 21:10:32 +0100, Frank Niessink <frank@niessink.com> wrote:

Franz Steinhaeusler schreef:

is there any volunteer, who wants to
following files
http://wiki.wxpython.org/index.cgi/WxHowtoSmallEditor

I updated the one above. Any comments?

Cheers, Frank

--
Franz Steinhaeusler