command line switches
huntermorgan
tompol at hotmail.com
Mon May 5 17:46:30 EDT 2003
hello long time viewer, first time poster
i have code in which i would like the functionality of comand line
switche(s).
below is the code. ive added it all so people may see what im doing.
and maybe supply code for command line switch functionality somewhere.
my thoughts are simply being able to add a command line switch that
switches between the turtle canvas drawer and the tkinter canvas
drawer. but how do i do this?
can anybody supply a simple example code that will work. something
simple that would enable a switch to be used to do something.
cheers everyone.
<code>
from Tkinter import *
from tkFileDialog import askopenfilename
from tkSimpleDialog import askstring
import tkMessageBox
import tkSimpleDialog
import turtle
import re
entryOpt = {
'Open File': askopenfilename,
'User Input': lambda: tkSimpleDialog.askstring('Enter
Instructions', 'Enter Drawer Instructions Here')
}
#create the GUI and use it to get the input from the user
class GUI(Frame):
def __init__(self, parent=None):
Frame.__init__(self, parent)
self.pack()
Label(self, text="Method of
Input",fg='black',font=('Helvetica', 12, 'italic bold')).pack()
for (key, value) in entryOpt.items():
func = (lambda self=self, name=key: self.getInput(name))
Button(self, text=key, command=func).pack(side=TOP,
fill=BOTH, ipady=5, pady=10)
def getInput(self, name):
inputString=entryOpt[name]()
inputType = re.compile('\w:')
inputType.match(inputString)
isUserInput = inputType.match(inputString)
if isUserInput == None:
pass
else:
fileName=inputString
file=open(fileName,'r').readlines()
for line in file:
inputString = line
# convert input to lowercase
inputString = inputString.lower()
s = SourceReader(inputString)
s.go(inputString)
class Drawer:
""" Responsible for defining an interface for drawing """
def __init__(self):
pass
def selectPen(self,penNum):
pass
def penDown(self):
pass
def penUp(self):
pass
def drawLine(self, direction, distance):
pass
class TkDrawer(Drawer):
# Using Tkinter canvas component to draw.
def getCanvas(self):
#Canvas.delete(drawingCanvas)
self.w = root
self.w.canvas = Canvas(drawingCanvas, bg='white')
self.w.canvas.pack(expand=YES, fill=BOTH)
def __init__(self):
self.getCanvas()
self.lineColor = "blue"
self.lineWidth = 1
self.startX = 150
self.startY = 150
self.toX = 150
self.toY = 150
def selectPen(self, penNum):
self.lineWidth = penNum
def penUp(self):
self.lineColor = "white"
def penDown(self):
self.lineColor = "blue"
def drawNorth(self, distance):
self.toX = self.startX
self.toY = self.startY - (distance * 10)
def drawEast(self, distance):
self.toX = self.startX + (distance * 10)
self.toY = self.startY
def drawSouth(self, distance):
self.toX = self.startX
self.toY = self.startY + (distance * 10)
def drawWest(self, distance):
self.toX = self.startX - (distance * 10)
self.toY = self.startY
def drawLine(self, direction, distance):
if direction == 0:
self.drawNorth(distance)
if direction == 180:
self.drawSouth(distance)
if direction == 90:
self.drawWest(distance)
if direction == 270:
self.drawEast(distance)
self.w.canvas.create_line(self.startX, self.startY,
self.toX, self.toY,
fill = self.lineColor,
width = self.lineWidth)
#setting the start position X & Y to the last stop of the
line.
self.startX = self.toX
self.startY = self.toY
class TurtleDrawer(Drawer):
def __init__(self):
turtle.reset()
def selectPen(self, penNum):
turtle.width(penNum)
def penDown(self):
turtle.down()
def penUp(self):
turtle.up()
def drawLine(self, direction, distance):
turtle.left(direction + 90)
turtle.forward(distance * 10)
turtle.right(direction + 90)
class Parser:
def __init__(self):
#self.drawer = TurtleDrawer()
self.source=[]
self.command=""
self.data=0
def isValidInput(self, inputString):
# check for valid characters
notAllowedChars = re.compile('[^pudnesw0-9\s]')
notAllowedChars.search(inputString)
isInvalidInput = notAllowedChars.search(inputString)
if isInvalidInput == None:
pass
else:
tkMessageBox.showerror('Invalid Input', 'The input is
invalid \nPlease check and try again')
inputString = "u"
# split inputString in to individual strings and create a
'list'
named "source"
tokenString = inputString
createTS = re.compile(r'\W')
token = createTS.split(tokenString)
self.source = token
self.parse(self.source)
def parse(self, rawSource):
self.source = rawSource
for character in self.source:
self.command=character[0]
try:
self.data=int(character[1])
except:
self.data=0
if self.command=="p":
self.drawer.selectPen(self.data)
if self.command=="d":
self.drawer.penDown()
if self.command=="n":
self.drawer.drawLine(0,self.data)
if self.command=="e":
self.drawer.drawLine(270,self.data)
if self.command=="s":
self.drawer.drawLine(180,self.data)
if self.command=="w":
self.drawer.drawLine(90,self.data)
if self.command=="u":
self.drawer.penUp()
class SourceReader:
""" responsible for providing source text for parsing and drawing
Initiates the Draw use-case.
Links to a parser and passes the source text onwards """
def __init__(self, inputString):
self.parser = Parser()
self.source = []
def go(self, inputString):
# keep interface, but class now has no real functionality
self.parser.isValidInput(inputString)
class Switch:
def TurtleDrawerButton(self):
Parser.drawer = TurtleDrawer()
def TkinterDrawCanvasButton(self):
Parser.drawer = TkDrawer()
#root frame and attributes
root = Tk()
root.title('PR301 Python Programming Assignment. March 2003')
root.resizable(width=NO, height=NO)
InstructMessage='''INSTRUCTIONS:\nOpen File OR To Input Drawing
Instructions:\nSelect Output Canvas\nUse the format 'p2 d w2 n1 e2 s1
u'\n
where 'p2' is pen width = 2, 'd' is place pen on canvas\n'n' is draw
north, 's' is draw south, 'e' is draw east, and 'w' is draw west\n
each number is the distance to move and 'u' raises the pen from the
canvas\n '''
Label(root, text=InstructMessage, bg='light
blue',fg='black',font=('Helvetica', 10, 'italic bold')).pack(side=TOP,
expand=YES, fill=BOTH)
Label(root, text='Interactive Front End' , bg='dark
blue',fg='white',font=('Helvetica', 12, 'italic bold')).pack(side=TOP,
fill=X)
#canvas
drawingCanvas=Canvas(root, background = 'dark grey')
drawingCanvas.pack(side=RIGHT, fill=BOTH)
Label(root, text="Select Canvas",fg='black',font=('Helvetica', 12,
'italic bold')).pack(side=TOP)
switch = Switch()
#turtle
trtlDrawer=Button(root, text='TurtleCanvas',
command=switch.TurtleDrawerButton)
trtlDrawer.pack(side=TOP, fill=BOTH, ipady=5, pady=10)
#tkinter
tkDrawer=Button(root,
text='TkinterCanvas',command=switch.TkinterDrawCanvasButton)
tkDrawer.pack(side=TOP, fill=BOTH, ipady=5, pady=10)
if __name__ == '__main__': GUI().mainloop()
</code>
More information about the Python-list
mailing list