Advertisement
here2share

# Tk_basic_paint_demo2.py

Aug 6th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. # Tk_basic_paint_demo2.py
  2.  
  3. from Tkinter import *
  4. root = Tk()
  5. root.geometry("500x500")
  6.  
  7. h = Scrollbar(root, orient=HORIZONTAL)
  8. v = Scrollbar(root, orient=VERTICAL)
  9. canvas = Canvas(root, scrollregion=(0, 0, 1000, 1000), yscrollcommand=v.set, xscrollcommand=h.set)
  10. h['command'] = canvas.xview
  11. v['command'] = canvas.yview
  12.  
  13. canvas.grid(column=0, row=0, sticky=(N,W,E,S))
  14. h.grid(column=0, row=1, sticky=(W,E))
  15. v.grid(column=1, row=0, sticky=(N,S))
  16. root.grid_columnconfigure(0, weight=1)
  17. root.grid_rowconfigure(0, weight=1)
  18.  
  19. lastx, lasty = 0, 0
  20.  
  21. import PIL.ImageGrab
  22. def xy(event):
  23.     global lastx, lasty, xyColor
  24.     lastx, lasty = canvas.canvasx(event.x), canvas.canvasy(event.y)
  25.     rgb = PIL.ImageGrab.grab().load()[event.x, event.y]
  26.     print "Clicked at: ", event.x, event.y
  27.     if event.x > 10 and event.x < 30 and event.y > 10 and event.y < 30+y:
  28.         #xyColor = '#%02x%02x%02x' % rgb ### ???
  29.         print colors[(event.y-10)/25]
  30.         xyColor = colors[(event.y-10)/25]
  31.         pass
  32.  
  33. def addLine(event):
  34.     global lastx, lasty
  35.     x, y = canvas.canvasx(event.x), canvas.canvasy(event.y)
  36.     if event.x > 40:
  37.         canvas.create_line((lastx, lasty, x, y), fill=xyColor, width=5, tags='currentline')
  38.         lastx, lasty = x, y
  39.     else: lastx, lasty = 40, y
  40.  
  41. def doneStroke(event):
  42.     canvas.itemconfigure('currentline', width=1)
  43.        
  44. canvas.bind("<Button-1>", xy)
  45. canvas.bind("<B1-Motion>", addLine)
  46. canvas.bind("<B1-ButtonRelease>", doneStroke)
  47.  
  48. colors = 'red orange yellow green blue purple pink brown white grey black'.split()
  49.  
  50. y=0
  51. for c in colors:
  52.     id = canvas.create_rectangle((10, 10+y, 30, 30+y), fill=c)
  53.     y+=25
  54.  
  55. xyColor='red'
  56. canvas.itemconfigure('palette', width=5)
  57. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement