import subprocess
import os



def main():
	quickfile = file_existance()
	quickdraw = subprocess.Popen(['java', '-jar', quickfile],\
						stdin = subprocess.PIPE, \
						stdout = subprocess.PIPE)
	event = WindowEvents(quickdraw)

def file_existance():
	filename = raw_input("Please enter the location of quickdraw: ")
	while (not os.path.isfile(filename)):
		print "That file does not exist, try again"
		filename = raw_input("Please enter the name of the file: ")
	print os.path.abspath(filename)
	return filename

def mouseclick(event):
	quickdraw.stdin.write("mouseclick true")
	quickdraw.stdin.write("\n")
	while(isQuickdrawClosed(event) == False):
		click = quickdraw.stdout.readline()
		print click

def isQuickdrawClosed(event) :
	if (not("Window" or "Mouse" in event)):
		raise ValueError, "this is not a legitimate event"
	else:
		if (len(event) > 0):
			x , y = event.split(":")
			y = y.strip()
			if (y == "Closed"):
				return True
			else: 
  				return False

def WindowEvents(quickdraw):
	shape = raw_input("What shape would you like to draw, choices are: square, circle or triangle ")
	quickdraw.stdin.write("windowevents true\n")
	quickdraw.stdin.write("mouseclick true\n")
	event = quickdraw.stdout.readline()
	if ("Window" in event):
		while (not isQuickdrawClosed(event)) :
			print event  
			event = quickdraw.stdout.readline()
	if ("Mouse" in event):
		while(isQuickdrawClosed(event) == False):
			event = quickdraw.stdout.readline()
			coordinates(quickdraw, event)
			if shape == "square":
				square(quickdraw, x, y)
			if shape == "circle":
				circle(quickdraw, x, y)
			print event

def coordinates(quickdraw, event):
	if ((len(event) > 0) and ("MousePressed" in event)):
		first  = event.split(":")[0]
		xcoord = event.split(":")[1]
		x = xcoord[0:4]		
		x = x.strip()
		y = xcoord[5:8]
		y = y.strip()
		print x
		print y
	return x
	return y


def circle(quickdraw, x, y):
	radius = 10
	while(radius < 400):
		circle = "circle" + ' ' + str(x) + ' ' + str(y) + ' ' + str(radius)
		quickdraw.stdin.write(circle)
		quickdraw.stdin.write("\n")
		radius= radius +10

def square(quickdraw, x, y):
	width = 10
	height = 10
	while (height and width < 800):
		square = "rect" + ' ' + str(x) + ' ' + str(y) + ' ' + str(width) + ' ' + str(height)
		quickdraw.stdin.write(square)
		quickdraw.stdin.write("\n")
		height = height + 10
		width = width + 10
		
main()

Traceback (most recent call last):
File "C:\Users\Gradey\Documents\PYTHON\test4.py", line 91, in <module>
main()
File "C:\Users\Gradey\Documents\PYTHON\test4.py", line 11, in main
event = WindowEvents(quickdraw)
File "C:\Users\Gradey\Documents\PYTHON\test4.py", line 52, in WindowEvents
coordinates(quickdraw, event)
File "C:\Users\Gradey\Documents\PYTHON\test4.py", line 69, in coordinates
return x
UnboundLocalError: local variable 'x' referenced before assignment

This is the error im getting. I want to run the x and y coordinates through to circle and square then run them into windowevents through the if statement but doesnt seem to work. I'm not sure whats wrong...Please help me.
Ps: x and y are both numbers just fyi.

Recommended Answers

All 19 Replies

Can't test it but it looks like bad indentation. Line it up like this.

def coordinates(quickdraw, event):
	if ((len(event) > 0) and ("MousePressed" in event)):
		first  = event.split(":")[0]
		xcoord = event.split(":")[1]
		x = xcoord[0:4]		
		x = x.strip()
		y = xcoord[5:8]
		y = y.strip()
		print x
		print y
	        return x
	        return y

If I do that, I get a global variable error instead of this:

Traceback (most recent call last):
File "C:\Users\Gradey\Documents\PYTHON\test4.py", line 91, in <module>
main()
File "C:\Users\Gradey\Documents\PYTHON\test4.py", line 11, in main
event = WindowEvents(quickdraw)
File "C:\Users\Gradey\Documents\PYTHON\test4.py", line 52, in WindowEvents
coordinates(quickdraw, event)
File "C:\Users\Gradey\Documents\PYTHON\test4.py", line 69, in coordinates
return x
UnboundLocalError: local variable 'x' referenced before assignment

Can you post that error as well?

woops sorry thought it was the right one, heres the error

Traceback (most recent call last):
File "C:\Users\Gradey\Documents\PYTHON\test4.py", line 91, in <module>
main()
File "C:\Users\Gradey\Documents\PYTHON\test4.py", line 11, in main
event = WindowEvents(quickdraw)
File "C:\Users\Gradey\Documents\PYTHON\test4.py", line 56, in WindowEvents
circle(quickdraw, x, y)
NameError: global name 'x' is not defined

When the if() statement is not executed, there is no "x" to return. Note also that the statement "return y" will never be reached/executed.

The if statement is defined when it equals mouseclicked though, so there is an x to return?
If return y will never be reached so i should put it as

return x, y
?

I believe this should do it.

You need to have a variable (x and y in this case) assigned to the coordinates function. The variable will equal whatever the function returns. It is not defined by just calling the function, as it doesn't have a variable to return a value to.

Change line 52 to:

x, y  = coordinates(quickdraw, event)

And change the returns in the coordinates function to:

return x, y

Traceback (most recent call last):
File "C:\Users\Gradey\Documents\PYTHON\test4.py", line 90, in <module>
main()
File "C:\Users\Gradey\Documents\PYTHON\test4.py", line 11, in main
event = WindowEvents(quickdraw)
File "C:\Users\Gradey\Documents\PYTHON\test4.py", line 52, in WindowEvents
x, y = coordinates(quickdraw, event)
TypeError: 'NoneType' object is not iterable

I get this now, after changing what you said

It didn't return anything (NoneType), so your if() statement turned out to be false. I don't know know quickdraw works and I can't test your code as I am on Windows. So you need to add another if(), elif(), or else() statement to do something when your initial if() statement is False.

When the if() statement is not executed, there is no "x" to return.

Okay awesome, that worked. But now it just continually draws circles until the restrictions are met. It doesnt let me click on a new x and y coordinate.. any suggestions?

The while() loop at line 50 doesn't seem to have any condition that stops it, other than when "isQuickdrawClosed" is True. So it'll keep going forever...and ever...and ever....:|

EDIT: I just noticed you're on Windows. I googled quickdraw and only Mac stuff came up. :P Maybe I can test it for you after all.

EDIT 2: Sure can. Can you give me your current code?

yeah for sure, haha yeah I was wondering why you couldnt test it.

import subprocess
import os

def main():
	quickfile = file_existance()
	quickdraw = subprocess.Popen(['java', '-jar', quickfile],\
						stdin = subprocess.PIPE, \
						stdout = subprocess.PIPE)
	event = WindowEvents(quickdraw)
	

def file_existance():
	filename = raw_input("Please enter the location of quickdraw: ")
	while (not os.path.isfile(filename)):
		print "That file does not exist, try again"
		filename = raw_input("Please enter the name of the file: ")
	print os.path.abspath(filename)
	return filename

def mouseclick(event):
	quickdraw.stdin.write("mouseclick true")
	quickdraw.stdin.write("\n")
	while(isQuickdrawClosed(event) == False):
		click = quickdraw.stdout.readline()
		print click

def isQuickdrawClosed(event) :
	if (not("Window" or "Mouse" in event)):
		raise ValueError, "this is not a legitimate event"
	else:
		if (len(event) > 0):
			x , y = event.split(":")
			y = y.strip()
			if (y == "Closed"):
				return True
			else: 
  				return False

def WindowEvents(quickdraw):
	shape = raw_input("What shape would you like to draw, choices are: square, circle or triangle ")
	quickdraw.stdin.write("windowevents true\n")
	quickdraw.stdin.write("mouseclick true\n")
	event = quickdraw.stdout.readline()
	if ("Window" in event):
		while (not isQuickdrawClosed(event)) :
			print event  
			event = quickdraw.stdout.readline()
	if ("Mouse" in event):
		while(isQuickdrawClosed(event) == False):
			event = quickdraw.stdout.readline()
			x, y =coordinates(quickdraw, event)
			while((shape == "square")):
				square(quickdraw, x, y)
			while((shape == "circle")):
				circle(quickdraw, x, y)
			print event
			

def coordinates(quickdraw, event):
	if ((len(event) > 0) and ("MouseReleased" in event)):
		first  = event.split(":")[0]
		xcoord = event.split(":")[1]
		x = xcoord[0:4]		
		x = x.strip()
		y = xcoord[5:8]
		y = y.strip()
		print x
		print y
		return x, y


def circle(quickdraw, x, y):
	radius =10
	if(radius < 400):
		circle = "circle" + ' ' + str(x) + ' ' + str(y) + ' ' + str(radius)
		quickdraw.stdin.write(circle)
		quickdraw.stdin.write("\n")
		radius = radius+10
		
		

def square(quickdraw, x, y):
	width = 10
	height = 10
	while (height and width < 800):
		square = "rect" + ' ' + str(x) + ' ' + str(y) + ' ' + str(width) + ' ' + str(height)
		quickdraw.stdin.write(square)
		quickdraw.stdin.write("\n")
		height = height + 10
		width = width + 10
	
	
main()

Thought it was for Mac, lol.

Yup you have a whole lot of while() loops that will never end. I changed your WindowEvents and coordinates functions again.

def WindowEvents(quickdraw):
	shape = raw_input("What shape would you like to draw, choices are: square, circle or triangle ")
	quickdraw.stdin.write("windowevents true\n")
	quickdraw.stdin.write("mouseclick true\n")
	event = quickdraw.stdout.readline()

	while 1: #Repeating these ifs over and over until one is true.
            if ("Window" in event):
                    while (not isQuickdrawClosed(event)) :
                            print event  
                            event = quickdraw.stdout.readline()
            if ("Mouse" in event):
                        event = quickdraw.stdout.readline()
                        x, y =coordinates(quickdraw, event)

                        if shape == "square": #while() not needed. will draw same square forever.
                            square(quickdraw, x, y)
                        if shape == "circle": #same as above. 
                            circle(quickdraw, x, y)
                        print event
			
                        #Now it will return to the top of the while 1 loop, allowing you to click and place another circle.

def coordinates(quickdraw, event):
	if ((len(event) > 0) and ("MouseReleased" in event)):
		first  = event.split(":")[0]
		xcoord = event.split(":")[1]
		x = xcoord[0:4]		
		x = x.strip()
		y = xcoord[5:8]
		y = y.strip()
		print x
		print y
		return x, y

	else: #Should probably change this
                return 0, 0

For whatever reason there is an error when you click the second time. So I added the else statement to put a circle at the very top left when this occurs.

ah okay awesome, see but i was also trying to get the circles to increase in size with every additional click, which is why i put the conditions in the circle(quickdraw,x,y) function with radius = raidus +10. It doesnt seem to get passed through however

It actually is passed through, but every time you call the function to draw a circle, radius is set back to 10. :)

EDIT: Moved 'radius' to outside the while loop, so it will never be set back to 10. Also added radius as an argument to the circle function.

Your circle function

def circle(quickdraw, x, y, radius):
	if(radius < 400):
		circle = "circle" + ' ' + str(x) + ' ' + str(y) + ' ' + str(radius)
		quickdraw.stdin.write(circle)
		quickdraw.stdin.write("\n")

And WindowEvents

def WindowEvents(quickdraw):
    shape = raw_input("What shape would you like to draw, choices are: square, circle or triangle ")
    quickdraw.stdin.write("windowevents true\n")
    quickdraw.stdin.write("mouseclick true\n")
    event = quickdraw.stdout.readline()

    radius = 10 
    while 1:
            if ("Window" in event):
                    while (not isQuickdrawClosed(event)) :
                            print event
                            event = quickdraw.stdout.readline()
            if ("Mouse" in event):
                        event = quickdraw.stdout.readline()
                        x, y = coordinates(quickdraw, event)

                        if shape == "square":
                            square(quickdraw, x, y)
                        if shape == "circle":
                            circle(quickdraw, x, y, radius) #added radius
                            radius = radius + 10
                        print event

Unfortunately the other circle in the top left also gets bigger...

should radius be passed into the if statement?

Your working code is above.

muchly appreciated, i'll figure out how to get rid of that top circle tomorrow. I've been staring at this code for a while. ty =D

You're very welcome.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.