Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.44 KB | None | 0 0
  1. # Made this program before I got out of my import * habit
  2. from tkinter import *
  3.  
  4. class SaveAsWindow:
  5.     '''Simple window, enter filename, and press save to export the canvas as a post script file viewable in acrobat'''
  6.     def remove_window(self):
  7.         '''Destroys the window'''
  8.         self.parent.saving = False
  9.         self.save_window.destroy()
  10.        
  11.     def save(self):
  12.         '''Saves the cavas as a ps file'''
  13.         self.file_name = self.entry.get()
  14.         save_file = self.parent.canvas.postscript(file = self.file_name + ".ps")
  15.         self.parent.saving = False
  16.         self.save_window.destroy()
  17.    
  18.     def __init__(self, parent):
  19.         '''Creates the window'''
  20.         self.parent = parent
  21.         if parent.saving == False:
  22.             parent.saving = True
  23.             self.save_window = Tk()
  24.             self.save_window.title("Save as...")
  25.             self.file_name = ""
  26.            
  27.             self.entry = Entry(self.save_window)
  28.             self.save_button = Button(self.save_window, text = "save", command = self.save)
  29.            
  30.             self.entry.grid(column = 0, row = 0)
  31.             self.save_button.grid(column = 1, row = 0)
  32.  
  33.             self.save_window.protocol("WM_DELETE_WINDOW", self.remove_window)
  34.  
  35. class Palate:
  36.     '''A window containing a grid of buttons to change the brush color'''
  37.     def remove_palate(self):
  38.         '''Destroys the window'''
  39.         self.parent.palate = False
  40.         self.color_grid.destroy()
  41.    
  42.     def __init__(self, parent):
  43.         '''Creates the window'''
  44.         self.parent = parent
  45.         if parent.palate == False:
  46.             parent.palate = True
  47.            
  48.             self.color_grid = Tk()
  49.            
  50.             self.black_button = Button(self.color_grid, bg="black", width = 3, command = lambda: self.parent.set_color("black"))
  51.             self.red_button = Button(self.color_grid, bg="red", width = 3, command = lambda: self.parent.set_color("red"))
  52.             self.blue_button = Button(self.color_grid, bg="blue", width = 3, command = lambda: self.parent.set_color("blue"))
  53.             self.green_button = Button(self.color_grid, bg="green", width = 3, command = lambda: self.parent.set_color("green"))
  54.             self.purple_button = Button(self.color_grid, bg="purple", width = 3, command = lambda: self.parent.set_color("purple"))
  55.             self.yellow_button = Button(self.color_grid, bg="yellow", width = 3, command = lambda: self.parent.set_color("yellow"))
  56.             self.orange_button = Button(self.color_grid, bg="orange", width = 3, command = lambda: self.parent.set_color("orange"))
  57.             self.hot_pink_button = Button(self.color_grid, bg="hot pink", width = 3, command = lambda: self.parent.set_color("hot pink"))
  58.             self.teal_button = Button(self.color_grid, bg="teal", width = 3, command = lambda: self.parent.set_color("teal"))
  59.             self.brown_button = Button(self.color_grid, bg="saddle brown", width = 3, command = lambda: self.parent.set_color("brown"))
  60.             self.grey_button = Button(self.color_grid, bg="dark grey", width = 3, command = lambda: self.parent.set_color("dark grey"))
  61.             self.lgreen_button = Button(self.color_grid, bg="lime green", width = 3, command = lambda: self.parent.set_color("lime green"))
  62.             self.sblue_button = Button(self.color_grid, bg="sky blue", width = 3, command = lambda: self.parent.set_color("sky blue"))
  63.             self.tan_button = Button(self.color_grid, bg="tan", width = 3, command = lambda: self.parent.set_color("tan"))
  64.             self.ygreen_button = Button(self.color_grid, bg="yellow green", width = 3, command = lambda: self.parent.set_color("yellow green"))
  65.             self.white_button = Button(self.color_grid, bg="white", width = 3, command = lambda: self.parent.set_color("white"))
  66.  
  67.             self.black_button.grid(row = 0, column = 0)
  68.             self.red_button.grid(row = 0, column = 1)
  69.             self.blue_button.grid(row = 0, column = 2)
  70.             self.teal_button.grid(row = 0, column = 3)
  71.             self.green_button.grid(row = 1, column = 0)
  72.             self.purple_button.grid(row = 1, column = 1)
  73.             self.yellow_button.grid(row = 1, column = 2)
  74.             self.brown_button.grid(row = 1, column = 3)
  75.             self.orange_button.grid(row = 2, column = 0)
  76.             self.hot_pink_button.grid(row = 2, column = 1)
  77.             self.white_button.grid(row = 2, column = 2)
  78.             self.grey_button.grid(row = 2, column = 3)
  79.             self.lgreen_button.grid(row = 3, column = 0)
  80.             self.sblue_button.grid(row = 3, column = 1)
  81.             self.tan_button.grid(row = 3, column = 2)
  82.             self.ygreen_button.grid(row = 3, column = 3)
  83.  
  84.             self.color_grid.protocol("WM_DELETE_WINDOW", self.remove_palate)
  85.  
  86. class MainWindow:
  87.     def close_all(self):
  88.         '''Closes all windows'''
  89.         if self.palate:
  90.             self.palate_window.remove_palate()
  91.         if self.saving:
  92.             self.save_window.remove_window()
  93.            
  94.         self.root.destroy()
  95.        
  96.     def set_color(self, new_color):
  97.         '''Sets the brush color to the chosen color'''
  98.         self.color = new_color
  99.  
  100.     def reduce_size(self):
  101.         '''Reduces the brush size by 2 if possible'''
  102.         if self.size > 2:
  103.             self.size -= 2
  104.             self.size_text.set(self.size)
  105.  
  106.     def increase_size(self):
  107.         '''Increases the brush size by 2'''
  108.         self.size += 2
  109.         self.size_text.set(str(self.size))
  110.  
  111.     def set_size(self, int_size):
  112.         '''Sets the brush to the requested size'''
  113.         self.size = int_size
  114.         self.size_text.set(str(int_size))
  115.  
  116.     def enable_draw(self, event):
  117.         '''Sets up the necessary variables to begin drawing on the canvas'''
  118.         size = self.size
  119.         x, y = event.x, event.y
  120.         self.last_x, self.last_y = x, y
  121.         self.brush_down = True
  122.         self.actions.append(self.canvas.create_oval(x-(size/2), y-(size/2), x+(size/2), y+(size/2), outline = self.color, fill = self.color))
  123.  
  124.     def disable_draw(self, event):
  125.         '''Disables canvas drawing'''
  126.         self.brush_down = False
  127.  
  128.     def update(self, event):
  129.         '''If you are not drawing, display the cursor, otherwise, draw a line from the last x,y to the new x,y'''
  130.         size = self.size
  131.         x, y = event.x, event.y
  132.         try:
  133.             self.canvas.delete(self.cursor)
  134.         finally:
  135.             if not self.brush_down:
  136.                 if self.color != "white":
  137.                     self.cursor = self.canvas.create_oval(x-(size/2), y-(size/2), x+(size/2), y+(size/2), outline = self.color)
  138.                 else:
  139.                     self.cursor = self.canvas.create_oval(x-(size/2), y-(size/2), x+(size/2), y+(size/2), outline = "black")
  140.             else:
  141.                 self.actions.append(self.canvas.create_line(x, y, self.last_x, self.last_y, width=size, fill = self.color))
  142.                 self.actions.append(self.canvas.create_oval(x-(size/2), y-(size/2), x+(size/2), y+(size/2), outline = self.color, fill = self.color))
  143.                 self.last_x, self.last_y = x, y
  144.  
  145.     def remove_cursor(self, event):
  146.         '''Remove the cursor'''
  147.         self.canvas.delete(self.cursor)
  148.  
  149.     def clear_canvas(self, event):
  150.         '''Clear all drawn objects on the canvas'''
  151.         self.canvas.delete(ALL)
  152.  
  153.     def save_window(self, event):
  154.         '''Create the save window'''
  155.         self.save_window = SaveAsWindow(self)
  156.  
  157.     def create_palate(self, event):
  158.         '''Create the palate window'''
  159.         self.palate_window = Palate(self)
  160.  
  161.     def check_brush_empty(self):
  162.         '''Checks if the user failed to input a new size'''
  163.         num = self.size_display.get()
  164.         if num == "":
  165.             self.size_text.set(self.size)
  166.  
  167.     def check_brush_change(self, event):
  168.         '''Checks if the user input a size that is invalid'''
  169.         num = self.size_display.get()
  170.         if num != "":
  171.             try:
  172.                 num = int(num)
  173.                 if num >= 1:
  174.                     self.set_size(num)
  175.                 else:
  176.                     self.size_text.set(size)
  177.             except:
  178.                 self.size_text.set(size)
  179.         else:
  180.             self.root.after(2000, check_brush_empty)
  181.            
  182.     def __init__(self):
  183.         '''Sets up the window'''
  184.         self.root = Tk()
  185.         self.color = "black"
  186.         self.size = 10
  187.         self.size_text = StringVar()
  188.         self.size_text.set(self.size)
  189.         self.brush_down = False
  190.         self.palate = False
  191.         self.saving = False
  192.         self.actions = []
  193.         self.cursor = 0
  194.         self.last_x, self.last_y = 0, 0
  195.        
  196.         self.palate_button = Button(self.root, text = "Create Palate", fg = "blue")
  197.         self.clear_button = Button(self.root, text = "Clear")
  198.         self.save_button = Button(self.root, text = "Save")
  199.         self.minus_size = Button(self.root, text = "<", command = self.reduce_size, width = 15)
  200.         self.size_display = Entry(self.root, textvariable = self.size_text, justify = CENTER)
  201.         self.add_size = Button(self.root, text = ">", command = self.increase_size, width = 15)
  202.         self.reset_size = Button(self.root, text = "Reset Brush", command = lambda: self.set_size(10))
  203.         self.canvas = Canvas(self.root, bg = "white", height = 500, width = 500, cursor="none")
  204.  
  205.         self.clear_button.grid(row = 0, column = 0, sticky = W+E)
  206.         self.save_button.grid(row = 0, column = 1, sticky = W+E)
  207.         self.palate_button.grid(row = 1, column = 0, sticky = W+E)
  208.         self.minus_size.grid(row = 1, column = 2, sticky = W)
  209.         self.size_display.grid(row = 1, column = 3, sticky = E+W)
  210.         self.add_size.grid(row = 1, column = 4, sticky = E)
  211.         self.reset_size.grid(row=0, column = 2, columnspan = 3, sticky = W+E)
  212.         self.canvas.grid(row = 2, column = 0, columnspan = 5, sticky = W+E)
  213.  
  214.         self.canvas.bind('<Motion>', self.update)
  215.         self.canvas.bind('<ButtonPress-1>', self.enable_draw)
  216.         self.canvas.bind('<ButtonRelease-1>', self.disable_draw)
  217.         self.canvas.bind('<Leave>', self.remove_cursor)
  218.         self.clear_button.bind('<Button-1>', self.clear_canvas)
  219.         self.save_button.bind('<Button-1>', self.save_window)
  220.         self.palate_button.bind('<Button-1>', self.create_palate)
  221.         self.size_display.bind('<KeyRelease>', self.check_brush_change)
  222.  
  223.         self.root.protocol("WM_DELETE_WINDOW", self.close_all)
  224.  
  225. app = MainWindow()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement