/Demo/tkinter/matt/canvas-reading-tag-info.py

http://unladen-swallow.googlecode.com/ · Python · 49 lines · 26 code · 11 blank · 12 comment · 0 complexity · f9f2345343ec605a3666382156346d7b MD5 · raw file

  1. from Tkinter import *
  2. class Test(Frame):
  3. def printit(self):
  4. print "hi"
  5. def createWidgets(self):
  6. self.QUIT = Button(self, text='QUIT', foreground='red',
  7. command=self.quit)
  8. self.QUIT.pack(side=BOTTOM, fill=BOTH)
  9. self.drawing = Canvas(self, width="5i", height="5i")
  10. # make a shape
  11. pgon = self.drawing.create_polygon(
  12. 10, 10, 110, 10, 110, 110, 10 , 110,
  13. fill="red", tags=("weee", "foo", "groo"))
  14. # this is how you query an object for its attributes
  15. # config options FOR CANVAS ITEMS always come back in tuples of length 5.
  16. # 0 attribute name
  17. # 1 BLANK
  18. # 2 BLANK
  19. # 3 default value
  20. # 4 current value
  21. # the blank spots are for consistency with the config command that
  22. # is used for widgets. (remember, this is for ITEMS drawn
  23. # on a canvas widget, not widgets)
  24. option_value = self.drawing.itemconfig(pgon, "stipple")
  25. print "pgon's current stipple value is -->", option_value[4], "<--"
  26. option_value = self.drawing.itemconfig(pgon, "fill")
  27. print "pgon's current fill value is -->", option_value[4], "<--"
  28. print " when he is usually colored -->", option_value[3], "<--"
  29. ## here we print out all the tags associated with this object
  30. option_value = self.drawing.itemconfig(pgon, "tags")
  31. print "pgon's tags are", option_value[4]
  32. self.drawing.pack(side=LEFT)
  33. def __init__(self, master=None):
  34. Frame.__init__(self, master)
  35. Pack.config(self)
  36. self.createWidgets()
  37. test = Test()
  38. test.mainloop()