How to write good Python objects?

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Fri Oct 24 05:24:50 EDT 2003


On Fri, 2003-10-24 at 02:15, John Ladasky wrote:
> I have written one nice, self-contained object that contained a DNA
> sequence, and various functions to manipulate the data therein.  My
> wxPython GUI objects, in contrast, are in a state of constant flux. 
> Rather than importing and reusing a piece of code, I find myself
> copying the code into my new program and playing with it, just a bit. 
> I'm starting to believe that writing a good GUI object, one that you
> can really reuse, is actually quite hard.  Yes, I know that you can
> derive a new object that overrides properties of your old object. 
> Should I find myself doing this for every object that I write?
> 

John ,


I see you've been helped with the /Lib folder question.  I am more
interested to hear about your GUI problem.  I don't use wxPython I use
Tkinter but hardly ever find myself copying code from one project to
another...(these days) perhaps you could share some examples of the kind
of GUI elements you find yourself copying...


One example I used to find myself copying would be a tool bar (Tkinter
does not have a 'toolbar' widget) I would find myself creating a Frame
then adding lots of buttons to it now however I use a Toolbar class I
created that has an add method that takes a couple of arguments.


e.g (untested) I now use something like:

class Toolbar(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        

    def add(self, label="", icon=None, command=None):
        b = Button(self, text=label, command=command, image=icon)
        b.pack(side="left")
        
... now to use it in some other module...

tb = Toolbar(root)
tb.pack(fill="x")

tools = [("Open", None, self.openFile),
    ("Close", None, self.closeFile),
    ("Quit", None, self.quit)
  ]
for label, image, command in tools:
    tb.add(label=label, icon=image, command=command)





## rather than...


toolbar = Frame(parent)
toolbar.pack()

openButton = Button(toolbar, text="Open", command=self.openFile)
openButton.pack(side="left")

closeButton = Button(toolbar, text="Close", command=self.closeFile)
closeButton.pack(side="left")

... you get the picture...


Is this the sort of problem you are having..?


Regards
Martin




-- 
Martin Franklin <mfranklin1 at gatwick.westerngeco.slb.com>






More information about the Python-list mailing list