Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###################################################################
- #
- # CSSE1001 - Assignment 2
- #
- # Student Number: 42642464
- #
- # Student Name: Victor Roussekov
- #
- ###################################################################
- #
- # Do not change the following import
- #
- from assign2_support import *
- ####################################################################
- #
- # Insert your code below
- #
- ####################################################################
- class PointFrame(Frame):
- """Widget for showing the cursor position of the last
- left mouse click as a point and the current cursor
- position as a point, both in real-world coordinates.
- """
- def __init__(self, master):
- """Initialize the values and labels:
- master: the toplevel window
- """
- self._master = master
- # Frame
- Frame.__init__(self, master)
- self.config(bg='dark grey')
- # Initial configuration
- self.lbl_lastpoint = Label(self, text="Last Point Clicked:",
- bg='dark grey')
- self.lastpoint = Label(self, text=' ', bg='dark grey')
- self.lbl_cursorpoint = Label(self, text="Cursor Point:",
- bg='dark grey')
- self.cursorpoint = Label(self, text='', bg='dark grey')
- # Packing
- self.lbl_lastpoint.pack(side=LEFT)
- self.lastpoint.pack(side=LEFT, padx=(0,100))
- self.lbl_cursorpoint.pack(side=LEFT)
- self.cursorpoint.pack(side=LEFT)
- class FunctionFrame(Frame):
- """Widget for entering the function and choosing the function colour"""
- def __init__(self, master, parent):
- """Initialize the frame and its contents
- master: the toplevel window"""
- self._master = master
- # Frame
- Frame.__init__(self, master)
- # Initial configuration
- self.bgcolour = 'light grey'
- self.lbl_function = Label(self, text="Function in x: ", bg=self.bgcolour)
- self.ent_function = Entry(self, width=40)
- self.lbl_funcolour = Label(self, text="Function Colour: ", bg=self.bgcolour)
- self.ent_funcolour = Entry(self, width=12)
- self.btn_selectcol = Button(self, width=5, text="Select", bg='white smoke',
- command=parent.set_colour)
- self.lbl_function.pack(side=LEFT)
- self.btn_selectcol.pack(side=RIGHT, padx=(4,4))
- self.ent_function.pack(side=LEFT)
- self.ent_funcolour.pack(side=RIGHT)
- self.lbl_funcolour.pack(side=RIGHT)
- def get_function(self):
- try:
- self.function_object = make_function(self.ent_function.get())
- except:
- self.error_message = self.ent_function.get(), " is not a valid function"
- tkMessageBox.showerror('', self.error_message)
- return None
- return self.function_object
- class PlotFrame(Frame):
- """Widget for entering the bounds of the plot and number of steps"""
- def __init__(self, master, parent):
- self._master = master
- self.bgcolour = 'light grey'
- # Frame
- Frame.__init__(self, master)
- # Initial configuration
- self.ent_width = 8
- self.lbl_plotset = Label(self, text="Plot Settings", bg=self.bgcolour)
- self.lbl_startx = Label(self, text="Start X:", bg=self.bgcolour)
- self.ent_startx = Entry(self, width=self.ent_width)
- self.lbl_endx = Label(self, text="End X:", bg=self.bgcolour)
- self.ent_endx = Entry(self, width=self.ent_width)
- self.lbl_starty = Label(self, text="Start Y:", bg=self.bgcolour)
- self.ent_starty = Entry(self, width=self.ent_width)
- self.lbl_endy = Label(self, text="End Y:", bg=self.bgcolour)
- self.ent_endy = Entry(self, width=self.ent_width)
- self.lbl_steps = Label(self, text="Steps:", bg=self.bgcolour)
- self.ent_steps = Entry(self, width=self.ent_width)
- # Packing
- self.lbl_plotset.pack(side=LEFT, padx=(0,25))
- self.lbl_startx.pack(side=LEFT)
- self.ent_startx.pack(side=LEFT, padx=(0,10))
- self.lbl_endx.pack(side=LEFT)
- self.ent_endx.pack(side=LEFT, padx=(0,10))
- self.lbl_starty.pack(side=LEFT)
- self.ent_starty.pack(side=LEFT, padx=(0,10))
- self.lbl_endy.pack(side=LEFT)
- self.ent_endy.pack(side=LEFT, padx=(0,10))
- self.lbl_steps.pack(side=LEFT)
- self.ent_steps.pack(side=LEFT)
- def get_values(self):
- # start x is not a float
- try:
- self.x1 = float(self.ent_startx.get())
- except:
- tkMessageBox.showerror('', "Start X must be a number")
- return None
- # end x is not a float
- try:
- self.x2 = float(self.ent_endx.get())
- except:
- tkMessageBox.showerror('', "End X must be a number")
- return None
- # start y is not a float
- try:
- self.y1 = float(self.ent_starty.get())
- except:
- tkMessageBox.showerror('', "Start Y must be a number")
- return None
- # end y is not a float
- try:
- self.y2 = float(self.ent_endy.get())
- except:
- tkMessageBox.showerror('', "End Y must be a number")
- return None
- # number of steps is an integer
- try:
- self.steps = int(self.ent_steps.get())
- except:
- tkMessageBox.showerror('', "Number of steps must be an integer")
- return None
- # the minimum x is bigger than the maximum x
- if self.x1 >= self.x2:
- tkMessageBox.showerror('', "End X must be greater than Start X")
- return None
- # the minimum y is bigger than the maximum y
- if self.y1 >= self.y2:
- tkMessageBox.showerror('', "End Y must be greater than Start Y")
- return None
- # the number of steps is positive
- if self.steps <= 0:
- tkMessageBox.showerror('', "Number of steps must be a positive")
- return None
- return (self.x1, self.x2, self.y1, self.y2, self.steps)
- class ButtonFrame(Frame):
- """Widget for creating the buttons"""
- def __init__(self, master, parent):
- self._master = master
- # Frame
- Frame.__init__(self, master)
- # Initial configuration
- self.bgcolour = 'white smoke'
- self.btn_addfun = Button(self, text="Add Function",
- command = parent.add_function, bg=self.bgcolour)
- self.btn_redrawfun = Button(self, text="Redraw All Functions",
- command = parent.redraw_all, bg=self.bgcolour)
- self.btn_removelast = Button(self, text="Remove Last Function",
- command = parent.remove_last, bg=self.bgcolour)
- self.btn_removeall = Button(self, text="Remove All Functions",
- command = parent.remove_all, bg=self.bgcolour)
- self.btn_quit = Button(self, text="Exit",
- command = parent.quit_app, bg=self.bgcolour)
- # Packing
- self.btn_addfun.pack(side=LEFT)
- self.btn_redrawfun.pack(side=LEFT)
- self.btn_removelast.pack(side=LEFT)
- self.btn_removeall.pack(side=LEFT)
- self.btn_quit.pack(side=LEFT)
- class PlotApp:
- def __init__(self, master):
- master.title("Function Plotter")
- self._master = master
- self._master.config(bg='dark grey')
- # Make the PointClass widget
- self.point_frame = PointFrame(master)
- self.point_frame.pack(anchor=W)
- # Create the canvas
- self.canvas = Canvas(master,
- bg='white',
- bd=2,
- relief=SUNKEN)
- master.minsize(700,480)
- master.geometry("800x600")
- self.canvas.pack(expand=1,fill=BOTH,padx=10)
- # Make the FunctionFrame widget
- self.function_frame = FunctionFrame(master, self)
- self.function_frame.configure(bg='light grey', bd=2, relief=SUNKEN)
- self.function_frame.pack(fill=BOTH, anchor=W, pady=10)
- # Make the PlotFrame widget
- self.plot_frame = PlotFrame(master, self)
- self.plot_frame.configure(bg='light grey', bd=2, relief=SUNKEN)
- self.plot_frame.pack(fill=BOTH, anchor=W, ipady=5, pady=10)
- # Make the ButtonFrame widget
- self.button_frame = ButtonFrame(master, self)
- self.button_frame.configure(bg='dark grey')
- self.button_frame.pack(side=LEFT)
- # Bindings
- self.canvas.bind("<Button-1>", self.press_button1)
- self.canvas.bind("<Motion>", self.motion)
- #self.canvas.bind("<Configure>", self.resize) -- implement later
- self.values = None
- self.function = None
- self.function_record = []
- self.function_objects = []
- self.colour = self.function_frame.ent_funcolour.insert(0, "black")
- def get_function(self):
- if self.function_frame.get_function():
- self.function = self.function_frame.get_function()
- self.function_objects.append(self.function)
- self.values = self.plot_frame.get_values()
- if self.values is not None:
- x1, x2, y1, y2, steps = self.values
- self.ws = WorldScreen(x1, y1, x2, y2,
- self.canvas.winfo_width(),
- self.canvas.winfo_height())
- if self.function is not None and self.values is not None:
- fun_list = list(FunctionIterator(self.function, x1,
- x2, steps))
- return fun_list
- return None
- def screen_function(self):
- screen_points = []
- fun_list = self.get_function()
- if fun_list is not None:
- for x,y in fun_list:
- screen_point = self.ws.world2screen(x, y)
- screen_points.append(screen_point)
- return screen_points
- return None
- # ButtonFrame functions
- def add_function(self):
- fill_colour = self.function_frame.ent_funcolour.get()
- screen_points = self.screen_function()
- if screen_points is not None:
- try:
- self.canvas.create_line(screen_points, fill=fill_colour)
- except Exception, e:
- tkMessageBox.showerror('', "Invalid colour")
- print e
- self.function_record.append((screen_points))
- return self.function_record
- return False
- def redraw_all(self):
- if self.add_function():
- self.canvas.delete(ALL)
- self.function_record = []
- values = self.plot_frame.get_values()
- screen_points = []
- real_fun = []
- if values is not None:
- x1, x2, y1, y2, steps = values
- self.ws = WorldScreen(x1, y1, x2, y2,
- self.canvas.winfo_width(),
- self.canvas.winfo_height())
- for function in self.function_objects:
- fun_list = list(FunctionIterator(function, x1,
- x2, steps))
- for x,y in fun_list:
- screen_point = self.ws.world2screen(x, y)
- real_fun.append(screen_point)
- self.function_record.append(real_fun)
- real_fun = []
- for fun in self.function_record:
- self.canvas.create_line(fun)
- def remove_last(self):
- if self.function_record:
- self.function_record.pop()
- self.canvas.delete(ALL)
- for function in self.function_record:
- self.canvas.create_line(function)
- def remove_all(self):
- self.canvas.delete(ALL)
- self.function_record = []
- def quit_app(self):
- self._master.destroy()
- # PointFrame functions
- def press_button1(self, e):
- if self.values is not None:
- points = self.ws.screen2world(e.x, e.y)
- self.x, self.y = points
- display = '({x: 5.2f}, {y: 5.2f})'.format(x=self.x,y=self.y)
- self.point_frame.lastpoint.configure(text=display)
- def motion(self, e):
- if self.values is not None:
- points = self.ws.screen2world(e.x, e.y)
- self.x,self.y = points
- display = '({x: 5.2f}, {y: 5.2f})'.format(x=self.x,y=self.y)
- self.point_frame.cursorpoint.configure(text=display)
- # FunctionFrame functions
- def set_colour(self):
- self.result = askcolor()
- self.function_frame.ent_funcolour.delete(0, END)
- self.function_frame.ent_funcolour.insert(0, self.result[1])
- ## def resize(self):
- ## deletes function / redraws
- ####################################################################
- #
- # WARNING: Leave the following code at the end of your code
- #
- # DO NOT CHANGE ANYTHING BELOW
- #
- ####################################################################
- def main():
- root = Tk()
- app = PlotApp(root)
- root.mainloop()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement