Need a help on my Python programming exam.
circ.db.db
circleserver(1).py
#!/usr/local/bin/python3 import wsgiref.simple_server import urllib.parse import sqlite3 import PIL.Image import PIL.ImageDraw def Mywebapp (environ, start_response) : if environ['REQUEST_METHOD'] == "GET" : if environ['PATH_INFO'] == '/' : response = """ <html> <head> <title>Circles Are Us</title> </head> <body> <h1> Welcome to the Circle Server </h1><br> <p> Welcome to our circle server. Please enter the criteria<br> for your circles below. </p> <form method="post"> Radius (5 - 50): <input type="range" name="radius" min="5" max="50" id="inputslider" value="25" oninput="outputslider.value=inputslider.value" > <output name="radiusout" id="outputslider" > </output><br><br> Color: <select name="color" > <option value="none"> None </option> <option value="red"> Red </option> <option value="green"> Green </option> <option value="blue"> Blue </option> <option value="orange"> Orange </option> <option value="purple"> Purple </option> <option value="pink"> Pink </option> <option value="yellow"> Yellow </option> <option value="black"> Black </option> </select><br><br> <input type="radio" name="fillval" value="1"> Filled<br> <input type="radio" name="fillval" value="0"> Not Filled<br> <input type="radio" name="fillval" value="-1" checked> None<br><br> <input type="submit" value="Get my circles"> </form> </body> </html>""" response = response.encode('utf-8') headers = [ ('Content-type', 'text/html, charset = utf-8'), ('Content-length', str(len(response))) ] else : try : response = open(environ['PATH_INFO'][1:], "rb").read() headers = [ ('Content-type', 'image/gif'), ('Content-length', str(len(response))) ] except : response = "Error - File not found" response = response.encode('utf-8') headers = [ ('Content-type', 'text/plain, charset = utf-8'), ('Content-length', str(len(response))) ] elif environ['REQUEST_METHOD'] == "POST" : resplen = environ['CONTENT_LENGTH'] data = environ['wsgi.input'].read(int(resplen)) data = data.decode('utf-8') datadict = urllib.parse.parse_qs(data) db = sqlite3.connect("circ.db") curs = db.cursor() query = "select * from circles where radius >= " + datadict['radius'][0] if datadict['color'][0] != "none" : query += " and color == '" + datadict['color'][0] + "'" if datadict['fillval'][0] != '-1' : query += " and fill == " + datadict['fillval'][0] circles = curs.execute(query) circleimage = PIL.Image.new('RGB', (640, 480), color="grey") drawer = PIL.ImageDraw.Draw(circleimage) for circle in circles : ulx = circle[0] - circle[2] uly = circle[1] - circle[2] lrx = circle[0] + circle[2] lry = circle[1] + circle[2] if circle[4] == 1 : drawer.ellipse((ulx,uly,lrx,lry), outline = circle[3], fill = circle[3]) else : drawer.ellipse((ulx,uly,lrx,lry), outline = circle[3]) circleimage.save("circimage.gif") response = """ <html> <head> <title>Circles Are Us</title> </head> <body> <h1> Welcome to the Circle Server </h1><br> <p> Below is an image with your requested circles </p> <form method="get"> <input type="submit" value="Return to previous page"> </form> <img src="circimage.gif" alt="circle image" width="640" height="480"/><br> </body> </html>""" response = response.encode('utf-8') headers = [ ('Content-type', 'text/html, charset = utf-8'), ('Content-length', str(len(response))) ] else : response = "Error - bad request type" response = response.encode('utf-8') headers = [ ('Content-type', 'text/plain'), ('Content-length', str(len(response))) ] status = "200 ok" start_response(status, headers) return [ response ] serv = wsgiref.simple_server.make_server("", 1234, Mywebapp) serv.serve_forever()
makecircles(1).py
#!/usr/local/bin/python3 import tkinter import tkinter.messagebox import math state = "nothing" moveit = False def hitbutton1 (ev) : global moveit global delid, centerx, centery if state == "add" : ulx = ev.x - radius.get() uly = ev.y - radius.get() lrx = ev.x + radius.get() lry = ev.y + radius.get() if checkfill.get() == 1 : fillit = color.get() else : fillit = "" circlecanvas.create_oval(ulx,uly,lrx,lry,outline = color.get(), fill = fillit) elif state == "delete" : delid = circlecanvas.find_closest(ev.x, ev.y) bbox = circlecanvas.coords(delid) centerx = (bbox[0] + bbox[2])/2 centery = (bbox[1] + bbox[3])/2 if math.sqrt((centerx - ev.x) ** 2 + (centery - ev.y) ** 2) < (centerx - bbox[0]) : circlecanvas.delete(delid) elif state == "move" : delid = circlecanvas.find_closest(ev.x, ev.y) bbox = circlecanvas.coords(delid) centerx = (bbox[0] + bbox[2])/2 centery = (bbox[1] + bbox[3])/2 if math.sqrt((centerx - ev.x) ** 2 + (centery - ev.y) ** 2) < (centerx - bbox[0]) : moveit = True else : moveit = False else : print("Ignoring mouse click") def hitbutton3 (ev) : global moveit if state == "move" and moveit == True : circlecanvas.move(delid, ev.x - centerx, ev.y - centery) moveit = False def hitkeya (ev) : global state state = "add" def hitkeyd (ev) : global state state = "delete" def hitkeym (ev) : global state state = "move" rootwindow = tkinter.Tk() rootwindow.title("Drawing Circles") rootwindow.lift() rootwindow.config(height = 200, width = 400, bg = "pink") radiuslabel = tkinter.Label(rootwindow, text = "Radius:", bg = "grey") radiuslabel.grid(row = 0, column = 0, sticky = "wens") rootwindow.columnconfigure(0, weight = 1) rootwindow.rowconfigure(0, weight = 1) radius = tkinter.IntVar() radiusslider = tkinter.Scale(rootwindow, from_ = 5, to = 50, variable = radius, bg = "green", orient = tkinter.HORIZONTAL) radiusslider.grid(row = 0, column = 1, sticky = "wens") rootwindow.columnconfigure(1, weight = 1) colorlabel = tkinter.Label(rootwindow, text = "Color:", bg = "grey") colorlabel.grid(row = 0, column = 2, sticky = "wens") rootwindow.columnconfigure(2, weight = 1) color = tkinter.StringVar() color.set("green") colors = [ "red", "green", "blue", "orange", "yellow", "purple", "pink" ] colormenu = tkinter.OptionMenu(rootwindow, color, *colors) colormenu.grid(row = 0, column = 3, sticky = "wens") rootwindow.columnconfigure(3, weight = 1) checkfill = tkinter.IntVar() fillcheck = tkinter.Checkbutton(rootwindow, text = "Fill Circle", variable = checkfill, bg = "grey") fillcheck.grid(row = 0, column = 4, sticky = "wens") rootwindow.columnconfigure(4, weight = 1) circlecanvas = tkinter.Canvas(rootwindow, height = 400, width = 600, bg = "grey") circlecanvas.grid(row = 1, column = 0, columnspan = 5, sticky = "wens") rootwindow.rowconfigure(1, weight = 4) circlecanvas.bind("<Button-1>", hitbutton1) circlecanvas.bind("<Button-2>", hitbutton3) circlecanvas.bind("a", hitkeya) circlecanvas.bind("d", hitkeyd) circlecanvas.bind("m", hitkeym) circlecanvas.focus_set() rootwindow.mainloop()
pongball(1).py
#!/usr/local/bin/python3 import tkinter import tkinter.messagebox import math import PIL.Image import PIL.ImageTk import time import random random.seed() i = 0 deltax = random.randint(1,3) deltay = random.randint(-3,3) def moveit2 () : global loadedimage3, tkimage3, i, deltax, deltay delay = 30 if i >= 24 : i = 0 balllocation = circlecanvas.coords(imageid) paddlelocation = circlecanvas.coords(paddleid) if balllocation[1] > paddlelocation[1] - 20 and balllocation[1] < paddlelocation[1] + 20 and balllocation[0] + 37 >= paddlelocation[0] - 2 : deltax = -deltax if balllocation[0] >= 565 : circlecanvas.coords(imageid, (300,200)) deltax = random.randint(1,3) deltay = random.randint(-3,3) delay = 1000 if balllocation[0] <= 37 : deltax = -deltax if balllocation[1] >= 365 : deltay = -deltay if balllocation[1] <= 37 : deltay = -deltay loadedimage3.seek(i) tkimage3 = PIL.ImageTk.PhotoImage(loadedimage3) circlecanvas.itemconfig(imageid, image = tkimage3) circlecanvas.move(imageid, deltax, deltay) i += 1 rootwindow.after(delay, moveit2) def moveit () : rootwindow.after(1000, moveit2) def hitkeyup (ev) : circlecanvas.move(paddleid, 0, -8) def hitkeydown (ev) : circlecanvas.move(paddleid, 0, 8) rootwindow = tkinter.Tk() rootwindow.title("Pong") rootwindow.lift() rootwindow.config(height = 200, width = 400, bg = "pink") loadedimage = PIL.Image.open("dummyimage.jpg") loadedimage.thumbnail((40,40)) tkimage = PIL.ImageTk.PhotoImage(loadedimage) radiuslabel = tkinter.Label(rootwindow, image = tkimage) radiuslabel.grid(row = 0, column = 0, sticky = "wens") rootwindow.columnconfigure(0, weight = 1) rootwindow.rowconfigure(0, weight = 1) loadedimage2 = PIL.Image.open("blob.png") loadedimage2.thumbnail((30,30)) tkimage2 = PIL.ImageTk.PhotoImage(loadedimage2) startbutton = tkinter.Button(rootwindow, image = tkimage2, command = moveit) startbutton.grid(row = 0, column = 1, sticky = "wens") rootwindow.columnconfigure(1, weight = 1) circlecanvas = tkinter.Canvas(rootwindow, height = 400, width = 600, bg = "grey") circlecanvas.grid(row = 1, column = 0, columnspan = 2, sticky = "wens") rootwindow.rowconfigure(1, weight = 4) loadedimage3 = PIL.Image.open("Downloads/animated-ball-image-0046.gif") tkimage3 = PIL.ImageTk.PhotoImage(loadedimage3) imageid = circlecanvas.create_image(300,200, image = tkimage3) paddleid = circlecanvas.create_image(500,200, image = tkimage) circlecanvas.bind("<Up>", hitkeyup) circlecanvas.bind("<Down>", hitkeydown) circlecanvas.focus_set() rootwindow.mainloop()
pongball.py
#!/usr/local/bin/python3 import tkinter import tkinter.messagebox import math import PIL.Image import PIL.ImageTk import time # circlecanvas.move(delid, ev.x - centerx, ev.y - centery) def moveit () : pass #def hitkeya (ev) : #global state #state = "add" rootwindow = tkinter.Tk() rootwindow.title("Pong") rootwindow.lift() rootwindow.config(height = 200, width = 400, bg = "pink") loadedimage = PIL.Image.open("dummyimage.jpg") loadedimage.thumbnail((40,40)) tkimage = PIL.ImageTk.PhotoImage(loadedimage) radiuslabel = tkinter.Label(rootwindow, image = tkimage) radiuslabel.grid(row = 0, column = 0, sticky = "wens") rootwindow.columnconfigure(0, weight = 1) rootwindow.rowconfigure(0, weight = 1) loadedimage2 = PIL.Image.open("blob.png") loadedimage2.thumbnail((30,30)) tkimage2 = PIL.ImageTk.PhotoImage(loadedimage2) startbutton = tkinter.Button(rootwindow, image = tkimage2, command = moveit) startbutton.grid(row = 0, column = 1, sticky = "wens") rootwindow.columnconfigure(1, weight = 1) circlecanvas = tkinter.Canvas(rootwindow, height = 400, width = 600, bg = "grey") circlecanvas.grid(row = 1, column = 0, columnspan = 2, sticky = "wens") rootwindow.rowconfigure(1, weight = 4) loadedimage3 = PIL.Image.open("Downloads/animated-ball-image-0046.gif") for i in range (0,24) : loadedimage3.seek(i) tkimage3 = PIL.ImageTk.PhotoImage(loadedimage3) circlecanvas.create_image(300,200, image = tkimage3) #circlecanvas.bind("<Button-1>", hitbutton1) #circlecanvas.bind("<Button-2>", hitbutton3) #circlecanvas.bind("a", hitkeya) #circlecanvas.bind("d", hitkeyd) #circlecanvas.bind("m", hitkeym) #circlecanvas.focus_set() rootwindow.mainloop()
selectcircles.py
#!/usr/local/bin/python3 import sqlite3 import random random.seed() db = sqlite3.connect("circles.db") curs = db.cursor() #curs.execute("create table circles (x integer, y integer, r integer, color text, fill integer)") #circlist = [] #for i in range(0,100) : #circlist.append((random.randint(0,600), random.randint(0,400), random.randint(5,50), random.choice(["red","blue","green","yellow","orange","purple","pink","black"]), random.randint(0,1))) #curs.executemany("insert into circles (x,y,r,color,fill) values (?,?,?,?,?)", circlist) #db.commit() data = curs.execute("select * from circles where r > 25 or fill == 1 order by r desc") for item in data : print("x =", item[0], "y =", item[1], "r =", item[2], "color =", item[3], "fill =", item[4]) db.close()
showcircles(1).py
#!/usr/local/bin/python3 import tkinter import tkinter.messagebox import math import sqlite3 import random random.seed() def createdb() : db = sqlite3.connect("circles.db") curs = db.cursor() curs.execute("create table if not exists circles (x integer, y integer, r integer, color text, fill integer)") circlist = [] for i in range(0,100) : circlist.append((random.randint(0,600), random.randint(0,400), random.randint(5,50), random.choice(["red","blue","green","yellow","orange","purple","pink","black"]), random.randint(0,1))) curs.executemany("insert into circles (x,y,r,color,fill) values (?,?,?,?,?)", circlist) db.commit() db.close() def selectdb() : db = sqlite3.connect("circles.db") curs = db.cursor() query = "select * from circles where r >= " + str(radius.get()) if color.get() != "none" : query += " and color == '" + color.get() + "'" if checkfill.get() != -1 : query += " and fill == " + str(checkfill.get()) circlist = curs.execute(query) circlecanvas.delete("all") for circ in circlist : ulx = circ[0] - circ[2] uly = circ[1] - circ[2] lrx = circ[0] + circ[2] lry = circ[1] + circ[2] if circ[4] : fillit = circ[3] else : fillit = "" circlecanvas.create_oval(ulx,uly,lrx,lry,outline = circ[3], fill = fillit) db.close() rootwindow = tkinter.Tk() rootwindow.title("Showing Circles") rootwindow.lift() rootwindow.config(height = 400, width = 600, bg = "pink") radiuslabel = tkinter.Label(rootwindow, text = "Radius:", bg = "grey") radiuslabel.grid(row = 0, column = 0, sticky = "wens") rootwindow.columnconfigure(0, weight = 1) rootwindow.rowconfigure(0, weight = 1) radius = tkinter.IntVar() radiusslider = tkinter.Scale(rootwindow, from_ = 5, to = 50, variable = radius, bg = "green", orient = tkinter.HORIZONTAL) radiusslider.grid(row = 0, column = 1, sticky = "wens") rootwindow.columnconfigure(1, weight = 1) colorlabel = tkinter.Label(rootwindow, text = "Color:", bg = "grey") colorlabel.grid(row = 0, column = 2, sticky = "wens") rootwindow.columnconfigure(2, weight = 1) color = tkinter.StringVar() color.set("none") colors = [ "none", "red", "green", "blue", "orange", "yellow", "purple", "pink", "black" ] colormenu = tkinter.OptionMenu(rootwindow, color, *colors) colormenu.grid(row = 0, column = 3, sticky = "wens") rootwindow.columnconfigure(3, weight = 1) checkfill = tkinter.IntVar() fillcheck1 = tkinter.Radiobutton(rootwindow, text = "None", variable = checkfill, value = -1, bg = "grey") fillcheck1.grid(row = 0, column = 4, sticky = "wens") rootwindow.columnconfigure(4, weight = 1) fillcheck2 = tkinter.Radiobutton(rootwindow, text = "Filled", variable = checkfill, value = 1, bg = "grey") fillcheck2.grid(row = 0, column = 5, sticky = "wens") rootwindow.columnconfigure(5, weight = 1) fillcheck3 = tkinter.Radiobutton(rootwindow, text = "Not Filled", variable = checkfill, value = 0, bg = "grey") fillcheck3.grid(row = 0, column = 6, sticky = "wens") rootwindow.columnconfigure(6, weight = 1) startbutton = tkinter.Button(rootwindow, text = "Start", command = createdb) startbutton.grid(row = 0, column = 7, sticky = "wens") rootwindow.columnconfigure(7, weight = 1) getbutton = tkinter.Button(rootwindow, text = "Select", command = selectdb) getbutton.grid(row = 0, column = 8, sticky = "wens") rootwindow.columnconfigure(8, weight = 1) circlecanvas = tkinter.Canvas(rootwindow, height = 400, width = 600, bg = "grey") circlecanvas.grid(row = 1, column = 0, columnspan = 9, sticky = "wens") rootwindow.rowconfigure(1, weight = 4) rootwindow.mainloop()
weathergui(1).py
#!/usr/local/bin/python3 import tkinter import tkinter.messagebox import urllib.request import json def getreport () : if len(zipcode.get()) == 5 and zipcode.get().isdigit() : requesturl = "http://api.openweathermap.org/data/2.5/weather?zip="+zipcode.get()+",us&mode=json&units=metric&APPID=b0d45b8cc18faf92d77e9825fd5aae74" try : urlobject = urllib.request.urlopen(requesturl) except BaseException as e: tkinter.messagebox.showerror("Url Error", "Bad Url" + e + ". Try again.") return if urlobject.getcode() == 200: response = urlobject.read() response = response.decode("utf-8") jsonresponse = json.loads(response) temperature = jsonresponse['main']['temp'] humidity = jsonresponse['main']['humidity'] pressure = jsonresponse['main']['pressure'] place = jsonresponse['name'] windspeed = jsonresponse['wind']['speed'] if 'deg' in jsonresponse['wind'].keys() : winddir = jsonresponse['wind']['deg'] windnames = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N' ] index = int((winddir + 11.25) / 22.5) windname = windnames[index] else : windname = "nowhere" outstring = "The weather conditions in " + place + "\n" outstring += "Temperature = " + str(temperature) + " degrees Celcius\n" outstring += "Humidity = " + str(humidity) + " %\n" outstring += "Barometric pressure = " + str(pressure) + " millibars\n" outstring += "Wind is at " + str(windspeed) + " km/h from " + windname weatherreport.set(outstring) else : tkinter.messagebox.showerror("Zipcode Error", "The value you entered -" + zipcode.get() + "- is not a valid zipcode. Try again.") rootwindow = tkinter.Tk() rootwindow.title("The Weather Reporter") rootwindow.lift() rootwindow.config(height = 200, width = 400, bg = "pink") instructions = tkinter.Label(rootwindow, text = "Welcome to the weather reporter. Please enter a zipcode for the location of your weather report.", wraplength = 300, justify = "left") instructions.grid(row = 0, column = 0, columnspan = 3, sticky = "wens") rootwindow.columnconfigure(0, weight = 1) rootwindow.rowconfigure(0, weight = 1) ziplabel = tkinter.Label(rootwindow, text = "Zipcode:", bg = "green") ziplabel.grid(row = 1, column = 0, sticky = "wens") rootwindow.rowconfigure(1, weight = 2) zipcode = tkinter.StringVar() zipentry = tkinter.Entry(rootwindow, bg = "blue", fg = "red", textvariable = zipcode) zipentry.grid(row = 1, column = 1, sticky = "wens") rootwindow.columnconfigure(1, weight = 1) gobutton = tkinter.Button(rootwindow, text = "Hit Me", command = getreport) gobutton.grid(row = 1, column = 2, sticky = "wens") rootwindow.columnconfigure(2, weight = 1) weatherreport = tkinter.StringVar() outputreport = tkinter.Message(rootwindow, textvariable = weatherreport, width=300) outputreport.grid(row = 2, column = 0, columnspan = 3, sticky = "wens") rootwindow.rowconfigure(2, weight = 3) rootwindow.mainloop()