#!/user/bin/python3 # -------------------------------------------------------------------- # basic graphics window object and methods # -------------------------------------------------------------------- import graphics as gr class graphics_window: '''Basic graphics window and methods (all coordinates are window coordinates)''' win_width = 801 win_height = 801 reset_button = [1, 1,80, 30] exit_button = [81,1,161,30] click_coords = [1,31,161,60] def __init__(self,title,width=win_width,height=win_height): self.title = title self.width = width self.height = height # ---- create window self.win = gr.GraphWin(title,width,height) self.win.setBackground("white") # ---- reset button b1 = gr.Rectangle(gr.Point(self.reset_button[0], self.reset_button[1]), gr.Point(self.reset_button[2], self.reset_button[3])) b1.setOutline('black') b1.setWidth(2) b1.draw(self.win) b1txt = gr.Text(b1.getCenter(),"Reset") b1txt.setStyle("bold") b1txt.draw(self.win) # ---- exit button b2 = gr.Rectangle(gr.Point(self.exit_button[0], self.exit_button[1]), gr.Point(self.exit_button[2], self.exit_button[3])) b2.setOutline('black') b2.setWidth(2) b2.draw(self.win) b1txt = gr.Text(b2.getCenter(),"Exit") b1txt.setStyle("bold") b1txt.draw(self.win) # ---- click coordinates ck = gr.Rectangle(gr.Point(self.click_coords[0], self.click_coords[1]), gr.Point(self.click_coords[2], self.click_coords[3])) ck.setOutline('black') ck.setWidth(2) ck.draw(self.win) cktxt = gr.Text(ck.getCenter(),f'Click Coords') cktxt.setStyle('bold') cktxt.draw(self.win) self.click_text = cktxt # ---- set click coordinates def set_click_coordinates(self,x,y): cktxt = self.click_text cktxt.undraw() cktxt.setText(f'x={x}, y={y}') cktxt.setStyle('bold') cktxt.draw(self.win) # ---- return graphics window object def win(): return self.win # ---- in reset button? def in_reset_button(self,x,y): if (x > self.reset_button[0]) and \ (x < self.reset_button[2]) and \ (y > self.reset_button[1]) and \ (y < self.reset_button[3]): return True return False # ---- in exit button? def in_exit_button(self,x,y): if (x > self.exit_button[0]) and \ (x < self.exit_button[2]) and \ (y > self.exit_button[1]) and \ (y < self.exit_button[3]): return True return False # ---- draw a point (small circle) def draw_point(self,x,y,color='red',size=4): p = gr.Circle(gr.Point(x,y),size) p.setFill(color) p.setOutline('black') p.draw(self.win) return p # ---- draw a line def draw_line(self,x1,y1,x2,y2,color='black',width=2): l = gr.Line(gr.Point(x1,y1),gr.Point(x2,y2)) l.setWidth(2) l.setFill(color) l.draw(self.win) return l # ---- draw (fancy) line segment def draw_line_segment(self,x1,y1,x2,y2,color='black',width=2, start_color='black',end_color='black',radius=4): l = gr.Line(gr.Point(x1,y1),gr.Point(x2,y2)) l.setWidth(width) l.setFill(color) l.draw(self.win) self.draw_point(x1,y1,start_color) self.draw_point(x2,y2,end_color) return l # -------------------------------------------------------------------- # ---- main # -------------------------------------------------------------------- if __name__ == '__main__': import sys import user_interface as ui import draw_xy_axes as ax import coordinate_conversion as cc gw = graphics_window('Basic Graphics Window') # ---- check for reset/exit button click # ---- display mouse click coordinates while(True): # ---- wait for mouse click and get coordinates # ---- was button clicked? set click coordinates click = gw.win.getMouse() x = click.getX() y = click.getY() if gw.in_exit_button(x,y): print('exit button') gw.win.close() sys.exit() if gw.in_reset_button(x,y): print('reset button') break gw.set_click_coordinates(x,y) #### #### do the rest of the demo code #### # ---- draw X,Y axes ax.draw_xy_axes(gw.win) # ---- draw a point (window coordinates) x = 300 y = 30 wx,wy = cc.center_to_win_coords(x,y,gw.width,gw.height) gw.draw_point(wx,wy) # ---- draw a line (window coordinates) gw.draw_line(200,200,400,400) # ---- draw a line segment (window coordinates) x1 = 51 y1 = 101 x2 = -201 y2 = -201 wx1,wy1 = cc.center_to_win_coords(x1,y1,gw.width,gw.height) wx2,wy2 = cc.center_to_win_coords(x2,y2,gw.width,gw.height) gw.draw_line_segment(wx1,wy1,wx2,wy2,width=4, end_color='red',start_color='yellow') # ---- wait for a mouse click, then exit click = gw.win.getMouse() gw.win.close()