Advertisement
here2share

# Tk_Widget_Snapshot.py

Nov 29th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. # Tk_Widget_Snapshot.py
  2.  
  3. from Tkinter import *
  4. from PIL import ImageGrab,ImageTk
  5. import time
  6.  
  7. root=Tk()
  8.  
  9. sq = 60
  10. midsq=sq/2
  11. numsq = 9
  12. numbers=range(1,10)
  13.  
  14. cvw,cvh=9*sq, sq
  15. canvas = Canvas(root,width=cvw,height=cvh)
  16.  
  17. for i in numbers:
  18.     canvas.create_text(sq*i-midsq,midsq, font=("Calibri", sq-8), text=i, fill='grey40')
  19. canvas.pack()
  20. canvas.update()
  21.  
  22. def grabwidget(widget,xy):
  23.     xx,yy,x2,y2=xy
  24.     x = widget.winfo_rootx()
  25.     y = widget.winfo_rooty()
  26.     w = widget.winfo_width()
  27.     h = widget.winfo_height()
  28.     time.sleep(0.3)
  29.     img=ImageGrab.grab((x+xx, y+yy, x+w+x2, y+h+y2))
  30.     return ImageTk.PhotoImage(img)
  31.  
  32. img9 = grabwidget(canvas,(0,0,sq*9,sq))
  33.  
  34. canvas.create_image(25,25, anchor=NW,image=img9) ### example made offset
  35.  
  36. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement