[Tutor] Program style

vicki@stanfield.net vicki@stanfield.net
Fri Mar 21 12:04:23 2003


This is a multi-part message in MIME format...

------------=_1048266195-12422-0
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: binary

I have a program which I have begun writing,
and the code works but is not in appropriate OOP format
(or doesn't seem to be). I am new to writing Python and
OOP, having written C programs and the like for several
years. If any of you experienced Python programmers
could take a quick look and advise me on how to get
this program into the appropriate style/format
before it gets so large that it is a major job to
revamp it, I would greatly appreciate it.

--vicki
 
P.S. I would like to create an __init__ function and
change the root stuff to self. Should this whole thing
be a class?
------------=_1048266195-12422-0
Content-Type: text/plain; name="ComTool.py"
Content-Disposition: attachment; filename="ComTool.py"
Content-Transfer-Encoding: 7bit

# File: ComTool.py

import Tkinter
import serial, stdio, os, sys, string

#Creates a callback shim based on code by Scott David Daniels
class SimpleCallback:
	def __init__(self, callback, *firstArgs):
		self.__callback = callback
		self.__firstArgs = firstArgs

	def __call__(self, *args):
		return self.__callback (*(self.__firstArgs + args))
	
def NewCallback():
    print "Hit new callback."

def OpenCallback():
    print "Hit open callback."
    
def HelpCallback():
    Helpwindow = Tkinter.Toplevel(root)
    Tkinter.Message(Helpwindow, background='White',text_color='DarkBlue',text= "You're beyond help!").pack()

def AboutCallback():
    
    Pmw.aboutversion('0.1')
    Pmw.aboutcopyright('Copyright Roche Diagnostics 2003\nAll rights reserved')
    Pmw.aboutcontact(
            'For information about this application contact:\n' +
            '  Vicki Stanfield\n' +
            '  Phone: 317-521-2378 \n' +
            '  email: vicki.stanfield@roche.com'
        )
    Aboutwindow = Tkinter.Toplevel(root)
    about = Pmw.AboutDialog(Aboutwindow, applicationname = 'CommTool')
    about.withdraw()

def EntryCallback(entry):
    value=entry.getvalue()
    print value
    if value:
	    SendCommand(value)
    
def SendCommand(command):
    for command in ('\x09','\x06'):
	port = serial.Serial(0, 9600, 8, 'N', 2, timeout=2)
	port.write(command)
	old=outputbox.getvalue()
	new=string.join([old, hex(ord(command))])
	outputbox.setvalue(new)
	input=port.read()
	print input
	if input:
		returnedval=hex(ord(input))
		if returnedval:
			print returnedval
			old=outputbox.getvalue()
			new=string.join([old, returnedval])
			outputbox.setvalue(new)
	port.close()

def CommandCallback(self):
    string=(Pmw.ScrolledListBox.getcurselection(self))[0]
    if string:
	    print string
    SendCommand(string)

def ReadAndClearStatus():
    port = serial.Serial(0, 9600, 8, 'N', 2, timeout=2)
    list="'\x09', 2, 2, '\x06', '\x15'"
    port.write(list)
    input=port.read()
    if input:
	    print hex(ord(input))
    port.close()

def ExitCallback():
    sys.exit(1)

#if __name__ == '__main__':
#    import sys
#    compare(sys.argv[1], sys.argv[2])


    
root=Tkinter.Tk()
root.title('SSS Communication Tool')

import Pmw
Pmw.initialise(root)

#stdio.setup_stdout()
#f = open('ComToolOutput', 'w')
#sys.stdout.redirect(f)
#stdio.setup_stdin()
#sys.stdin.redirect(f)

# create a menu
menu=Tkinter.Menu(root)
root.config(menu=menu)

Filemenu = Tkinter.Menu(menu)
menu.add_cascade(label="File", menu=Filemenu)
Filemenu.add_command(label="New", command=NewCallback)
Filemenu.add_command(label="Open...", command=OpenCallback)
Filemenu.add_separator()
Filemenu.add_command(label="Exit", command=ExitCallback)

Helpmenu = Tkinter.Menu(menu)
menu.add_cascade(label="Help", menu=Helpmenu)
Helpmenu.add_command(label="Help...", command=HelpCallback)
Helpmenu.add_command(label="About...", command=AboutCallback)


Frame1=Tkinter.Frame(root)
Frame1.pack()

Frame2=Tkinter.Frame(Frame1)
Frame2.pack()

# Create the Command Window.
outputbox=Pmw.ScrolledText(Frame2,
			   labelpos = 'n',
			   label_text = 'Command Window',
			   vscrollmode='static')
outputbox.pack(padx = 38, pady = 38)
outputbox.setvalue('test')

entry = Pmw.EntryField(Frame2, label_text='Enter command:',labelpos='w')
callback=SimpleCallback(EntryCallback,entry)
entry.configure(command=callback)
entry.pack(pady=25)

separator1 = Tkinter.Frame(relief="ridge", height=4, bg="darkgrey")
separator1.pack(padx=5, pady=5)
    
Frame3=Tkinter.Frame(Frame1)
Frame3.pack()

Listbox1=Pmw.ScrolledListBox(Frame3, 
		listbox_height = 5,labelpos='w',label_text='Select Command',
                items=("06", "09", "10", "1D", "5A", "0B", "58", "43", "49", "53", "0C", "60", "61", "4D", "57", "56", "11"))
callback=SimpleCallback(CommandCallback,Listbox1)
Listbox1.configure(selectioncommand=callback)
Listbox1.pack(padx=20)

Pmw.alignlabels([entry,Listbox1])

Tkinter.mainloop()

------------=_1048266195-12422-0--