Format for Python/Tkinter program

Vandl vicki at stanfield.net
Fri Mar 21 15:00:50 EST 2003


I am new to Python/Tkinter, having programmed quite a bit in C/Motif.
I am trying to get a feel for the appropriate format for my new
programs. Here is a program which I am beginning to develop. Can I get
some tips on what is non-standard in what I am doing. Also, is it
appropriate to have a main program and header files P/T? Should my
functions be in a separate file from the code that runs them? Here is
some code that I am working on; any hints on how to set it up are
welcome. It is, as you can see, very much in draft form.

-----------
# 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 Some Person 2003\n' +
				'All rights reserved')
	Pmw.aboutcontact(
            		'For information about this application contact:\n' +
			'  Some Person\n' +
			'  email: someperson at domain.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 ("first,"second"):
		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)
    
root=Tkinter.Tk()
root.title('SSS Communication Tool')

import Pmw
Pmw.initialise(root)

# 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=("first", "second", "third", "fourth", "fifth"))
callback=SimpleCallback(CommandCallback,Listbox1)
Listbox1.configure(selectioncommand=callback)
Listbox1.pack(padx=20)

Pmw.alignlabels([entry,Listbox1])

Tkinter.mainloop()
---------------------

--vicki




More information about the Python-list mailing list