Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.74 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. ###################################################################
  6. #
  7. #   CSSE1001 - Assignment 2
  8. #
  9. #   Student Number: 42642464
  10. #
  11. #   Student Name: Victor Roussekov
  12. #
  13. ###################################################################
  14.  
  15.  
  16. #
  17. # Do not change the following import
  18. #
  19.  
  20. from assign2_support import *
  21.  
  22. ####################################################################
  23. #
  24. # Insert your code below
  25. #
  26. ####################################################################
  27.  
  28. class PointFrame(Frame):
  29.     """Widget for showing the cursor position of the last
  30.    left mouse click as a point and the current cursor
  31.    position as a point, both in real-world coordinates.
  32.    """
  33.    
  34.     def __init__(self,  master):
  35.         """Initialize the values and labels:
  36.        master: the toplevel window
  37.        """
  38.         self._master = master
  39.        
  40.         # Frame
  41.         Frame.__init__(self, master)
  42.         self.config(bg='dark grey')
  43.  
  44.         # Initial configuration        
  45.         self.lbl_lastpoint = Label(self, text="Last Point Clicked:",
  46.                                    bg='dark grey')
  47.         self.lastpoint = Label(self, text='                      ', bg='dark grey')
  48.        
  49.         self.lbl_cursorpoint = Label(self, text="Cursor Point:",
  50.                                      bg='dark grey')
  51.         self.cursorpoint = Label(self, text='', bg='dark grey')
  52.  
  53.         # Packing
  54.         self.lbl_lastpoint.pack(side=LEFT)
  55.         self.lastpoint.pack(side=LEFT, padx=(0,100))
  56.         self.lbl_cursorpoint.pack(side=LEFT)
  57.         self.cursorpoint.pack(side=LEFT)
  58.        
  59. class FunctionFrame(Frame):
  60.     """Widget for entering the function and choosing the function colour"""
  61.  
  62.     def __init__(self, master, parent):
  63.         """Initialize the frame and its contents
  64.        master: the toplevel window"""
  65.        
  66.         self._master = master
  67.        
  68.         # Frame
  69.         Frame.__init__(self, master)
  70.  
  71.         # Initial configuration
  72.         self.bgcolour = 'light grey'
  73.         self.lbl_function = Label(self, text="Function in x: ", bg=self.bgcolour)
  74.         self.ent_function = Entry(self, width=40)
  75.         self.lbl_funcolour = Label(self, text="Function Colour: ", bg=self.bgcolour)
  76.         self.ent_funcolour = Entry(self, width=12)
  77.         self.btn_selectcol = Button(self, width=5, text="Select", bg='white smoke',
  78.                                     command=parent.set_colour)
  79.         self.lbl_function.pack(side=LEFT)
  80.        
  81.         self.btn_selectcol.pack(side=RIGHT, padx=(4,4))
  82.         self.ent_function.pack(side=LEFT)
  83.        
  84.         self.ent_funcolour.pack(side=RIGHT)
  85.         self.lbl_funcolour.pack(side=RIGHT)
  86.  
  87.     def get_function(self):
  88.         try:
  89.             self.function_object = make_function(self.ent_function.get())
  90.         except:
  91.             self.error_message = self.ent_function.get(), " is not a valid function"
  92.             tkMessageBox.showerror('', self.error_message)
  93.             return None
  94.         return self.function_object
  95.        
  96. class PlotFrame(Frame):
  97.     """Widget for entering the bounds of the plot and number of steps"""
  98.  
  99.     def __init__(self, master, parent):
  100.  
  101.         self._master = master
  102.         self.bgcolour = 'light grey'
  103.  
  104.         # Frame        
  105.         Frame.__init__(self, master)
  106.  
  107.         # Initial configuration
  108.         self.ent_width = 8
  109.        
  110.         self.lbl_plotset = Label(self, text="Plot Settings", bg=self.bgcolour)
  111.         self.lbl_startx = Label(self, text="Start X:", bg=self.bgcolour)
  112.         self.ent_startx = Entry(self, width=self.ent_width)
  113.         self.lbl_endx = Label(self, text="End X:", bg=self.bgcolour)
  114.         self.ent_endx = Entry(self, width=self.ent_width)
  115.         self.lbl_starty = Label(self, text="Start Y:", bg=self.bgcolour)
  116.         self.ent_starty = Entry(self, width=self.ent_width)
  117.         self.lbl_endy = Label(self, text="End Y:", bg=self.bgcolour)
  118.         self.ent_endy = Entry(self, width=self.ent_width)
  119.         self.lbl_steps = Label(self, text="Steps:", bg=self.bgcolour)
  120.         self.ent_steps = Entry(self, width=self.ent_width)
  121.  
  122.         # Packing
  123.         self.lbl_plotset.pack(side=LEFT, padx=(0,25))
  124.         self.lbl_startx.pack(side=LEFT)
  125.         self.ent_startx.pack(side=LEFT, padx=(0,10))
  126.         self.lbl_endx.pack(side=LEFT)
  127.         self.ent_endx.pack(side=LEFT, padx=(0,10))
  128.         self.lbl_starty.pack(side=LEFT)
  129.         self.ent_starty.pack(side=LEFT, padx=(0,10))
  130.         self.lbl_endy.pack(side=LEFT)
  131.         self.ent_endy.pack(side=LEFT, padx=(0,10))
  132.         self.lbl_steps.pack(side=LEFT)
  133.         self.ent_steps.pack(side=LEFT)
  134.        
  135.     def get_values(self):
  136.  
  137.         # start x is not a float
  138.         try:
  139.             self.x1 = float(self.ent_startx.get())
  140.         except:
  141.             tkMessageBox.showerror('', "Start X must be a number")
  142.             return None
  143.  
  144.         # end x is not a float
  145.         try:
  146.             self.x2 = float(self.ent_endx.get())
  147.         except:
  148.             tkMessageBox.showerror('', "End X must be a number")
  149.             return None
  150.  
  151.         # start y is not a float
  152.         try:
  153.             self.y1 = float(self.ent_starty.get())
  154.         except:
  155.             tkMessageBox.showerror('', "Start Y must be a number")
  156.             return None
  157.  
  158.         # end y is not a float
  159.         try:
  160.             self.y2 = float(self.ent_endy.get())
  161.         except:
  162.             tkMessageBox.showerror('', "End Y must be a number")
  163.             return None
  164.        
  165.         # number of steps is an integer
  166.         try:
  167.             self.steps = int(self.ent_steps.get())
  168.         except:
  169.             tkMessageBox.showerror('', "Number of steps must be an integer")
  170.             return None
  171.  
  172.         # the minimum x is bigger than the maximum x
  173.         if self.x1 >= self.x2:
  174.             tkMessageBox.showerror('', "End X must be greater than Start X")
  175.             return None
  176.        
  177.         # the minimum y is bigger than the maximum y
  178.         if self.y1 >= self.y2:
  179.             tkMessageBox.showerror('', "End Y must be greater than Start Y")
  180.             return None
  181.        
  182.         # the number of steps is positive
  183.         if self.steps <= 0:
  184.             tkMessageBox.showerror('', "Number of steps must be a positive")
  185.             return None
  186.                                
  187.         return (self.x1, self.x2, self.y1, self.y2, self.steps)
  188.  
  189. class ButtonFrame(Frame):
  190.     """Widget for creating the buttons"""
  191.     def __init__(self, master, parent):
  192.         self._master = master
  193.  
  194.         # Frame        
  195.         Frame.__init__(self, master)
  196.  
  197.         # Initial configuration
  198.         self.bgcolour = 'white smoke'
  199.         self.btn_addfun = Button(self, text="Add Function",                                
  200.                                  command = parent.add_function, bg=self.bgcolour)
  201.         self.btn_redrawfun = Button(self, text="Redraw All Functions",                                
  202.                                  command = parent.redraw_all, bg=self.bgcolour)
  203.         self.btn_removelast = Button(self, text="Remove Last Function",                                
  204.                                  command = parent.remove_last, bg=self.bgcolour)
  205.         self.btn_removeall = Button(self, text="Remove All Functions",                                
  206.                                  command = parent.remove_all, bg=self.bgcolour)
  207.         self.btn_quit = Button(self, text="Exit",                                  
  208.                                  command = parent.quit_app, bg=self.bgcolour)
  209.  
  210.         # Packing
  211.         self.btn_addfun.pack(side=LEFT)
  212.         self.btn_redrawfun.pack(side=LEFT)
  213.         self.btn_removelast.pack(side=LEFT)
  214.         self.btn_removeall.pack(side=LEFT)
  215.         self.btn_quit.pack(side=LEFT)
  216.  
  217. class PlotApp:
  218.     def __init__(self, master):
  219.         master.title("Function Plotter")
  220.         self._master = master
  221.         self._master.config(bg='dark grey')
  222.  
  223.         # Make the PointClass widget
  224.         self.point_frame = PointFrame(master)
  225.         self.point_frame.pack(anchor=W)  
  226.  
  227.         # Create the canvas      
  228.         self.canvas = Canvas(master,
  229.                              bg='white',
  230.                              bd=2,
  231.                              relief=SUNKEN)
  232.        
  233.         master.minsize(700,480)
  234.         master.geometry("800x600")
  235.        
  236.         self.canvas.pack(expand=1,fill=BOTH,padx=10)
  237.  
  238.         # Make the FunctionFrame widget
  239.         self.function_frame = FunctionFrame(master, self)
  240.         self.function_frame.configure(bg='light grey', bd=2, relief=SUNKEN)
  241.         self.function_frame.pack(fill=BOTH, anchor=W, pady=10)
  242.  
  243.         # Make the PlotFrame widget
  244.         self.plot_frame = PlotFrame(master, self)
  245.         self.plot_frame.configure(bg='light grey', bd=2, relief=SUNKEN)
  246.         self.plot_frame.pack(fill=BOTH, anchor=W, ipady=5, pady=10)
  247.  
  248.         # Make the ButtonFrame widget
  249.         self.button_frame = ButtonFrame(master, self)
  250.         self.button_frame.configure(bg='dark grey')
  251.         self.button_frame.pack(side=LEFT)
  252.  
  253.         # Bindings    
  254.         self.canvas.bind("<Button-1>", self.press_button1)
  255.         self.canvas.bind("<Motion>", self.motion)
  256.         #self.canvas.bind("<Configure>", self.resize) -- implement later
  257.  
  258.         self.values = None
  259.         self.function = None
  260.         self.function_record = []
  261.         self.function_objects = []
  262.         self.colour = self.function_frame.ent_funcolour.insert(0, "black")
  263.  
  264.     def get_function(self):
  265.         if self.function_frame.get_function():
  266.             self.function = self.function_frame.get_function()
  267.             self.function_objects.append(self.function)
  268.             self.values = self.plot_frame.get_values()
  269.    
  270.         if self.values is not None:
  271.             x1, x2, y1, y2, steps = self.values
  272.  
  273.             self.ws = WorldScreen(x1, y1, x2, y2,
  274.                           self.canvas.winfo_width(),
  275.                           self.canvas.winfo_height())
  276.  
  277.         if self.function is not None and self.values is not None:
  278.             fun_list = list(FunctionIterator(self.function, x1,
  279.                                                   x2, steps))
  280.            
  281.             return fun_list
  282.         return None
  283.  
  284.     def screen_function(self):
  285.         screen_points = []
  286.         fun_list = self.get_function()
  287.         if fun_list is not None:
  288.             for x,y in fun_list:
  289.                     screen_point = self.ws.world2screen(x, y)
  290.                     screen_points.append(screen_point)
  291.  
  292.             return screen_points
  293.         return None
  294.        
  295.     # ButtonFrame functions
  296.     def add_function(self):
  297.             fill_colour = self.function_frame.ent_funcolour.get()
  298.             screen_points = self.screen_function()
  299.             if screen_points is not None:
  300.                 try:
  301.                     self.canvas.create_line(screen_points, fill=fill_colour)
  302.                 except Exception, e:
  303.                     tkMessageBox.showerror('', "Invalid colour")
  304.                     print e
  305.                 self.function_record.append((screen_points))
  306.  
  307.                 return self.function_record
  308.             return False
  309.  
  310.     def redraw_all(self):
  311.         if self.add_function():
  312.             self.canvas.delete(ALL)
  313.             self.function_record = []
  314.             values = self.plot_frame.get_values()
  315.             screen_points = []
  316.             real_fun = []
  317.        
  318.         if values is not None:
  319.             x1, x2, y1, y2, steps = values
  320.  
  321.             self.ws = WorldScreen(x1, y1, x2, y2,
  322.                           self.canvas.winfo_width(),
  323.                           self.canvas.winfo_height())
  324.  
  325.         for function in self.function_objects:
  326.             fun_list = list(FunctionIterator(function, x1,
  327.                                                   x2, steps))
  328.             for x,y in fun_list:
  329.                 screen_point = self.ws.world2screen(x, y)
  330.                 real_fun.append(screen_point)
  331.             self.function_record.append(real_fun)
  332.             real_fun = []
  333.  
  334.         for fun in self.function_record:
  335.             self.canvas.create_line(fun)
  336.  
  337.                
  338.     def remove_last(self):
  339.         if self.function_record:
  340.             self.function_record.pop()
  341.             self.canvas.delete(ALL)
  342.             for function in self.function_record:
  343.                 self.canvas.create_line(function)
  344.        
  345.     def remove_all(self):
  346.         self.canvas.delete(ALL)
  347.         self.function_record = []
  348.  
  349.     def quit_app(self):
  350.         self._master.destroy()
  351.  
  352.     # PointFrame functions
  353.     def press_button1(self, e):
  354.         if self.values is not None:
  355.             points = self.ws.screen2world(e.x, e.y)
  356.             self.x, self.y = points
  357.             display = '({x: 5.2f}, {y: 5.2f})'.format(x=self.x,y=self.y)
  358.             self.point_frame.lastpoint.configure(text=display)
  359.  
  360.     def motion(self, e):
  361.         if self.values is not None:
  362.             points = self.ws.screen2world(e.x, e.y)
  363.             self.x,self.y = points
  364.             display = '({x: 5.2f}, {y: 5.2f})'.format(x=self.x,y=self.y)
  365.             self.point_frame.cursorpoint.configure(text=display)
  366.  
  367.     # FunctionFrame functions
  368.     def set_colour(self):
  369.         self.result = askcolor()
  370.         self.function_frame.ent_funcolour.delete(0, END)
  371.         self.function_frame.ent_funcolour.insert(0, self.result[1])            
  372.  
  373. ##    def resize(self):
  374. ##        deletes function / redraws
  375.        
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385. ####################################################################
  386. #
  387. # WARNING: Leave the following code at the end of your code
  388. #
  389. # DO NOT CHANGE ANYTHING BELOW
  390. #
  391. ####################################################################
  392.  
  393. def main():
  394.     root = Tk()
  395.     app = PlotApp(root)
  396.     root.mainloop()
  397.  
  398. if  __name__ == '__main__':
  399.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement