/Demo/tkinter/matt/canvas-w-widget-draw-el.py

http://unladen-swallow.googlecode.com/ · Python · 36 lines · 19 code · 10 blank · 7 comment · 0 complexity · b6bb9e549424dab78e344905cdc34d36 MD5 · raw file

  1. from Tkinter import *
  2. # this file demonstrates the creation of widgets as part of a canvas object
  3. class Test(Frame):
  4. def printhi(self):
  5. print "hi"
  6. def createWidgets(self):
  7. self.QUIT = Button(self, text='QUIT', foreground='red',
  8. command=self.quit)
  9. self.QUIT.pack(side=BOTTOM, fill=BOTH)
  10. self.draw = Canvas(self, width="5i", height="5i")
  11. self.button = Button(self, text="this is a button",
  12. command=self.printhi)
  13. # note here the coords are given in pixels (form the
  14. # upper right and corner of the window, as usual for X)
  15. # but might just have well been given in inches or points or
  16. # whatever...use the "anchor" option to control what point of the
  17. # widget (in this case the button) gets mapped to the given x, y.
  18. # you can specify corners, edges, center, etc...
  19. self.draw.create_window(300, 300, window=self.button)
  20. self.draw.pack(side=LEFT)
  21. def __init__(self, master=None):
  22. Frame.__init__(self, master)
  23. Pack.config(self)
  24. self.createWidgets()
  25. test = Test()
  26. test.mainloop()