/Demo/tkinter/matt/packer-and-placer-together.py

http://unladen-swallow.googlecode.com/ · Python · 41 lines · 17 code · 11 blank · 13 comment · 0 complexity · db9d4d741a285eeb149ee7da5eaa0b9c MD5 · raw file

  1. from Tkinter import *
  2. # This is a program that tests the placer geom manager in conjunction with
  3. # the packer. The background (green) is packed, while the widget inside is placed
  4. def do_motion(event):
  5. app.button.place(x=event.x, y=event.y)
  6. def dothis():
  7. print 'calling me!'
  8. def createWidgets(top):
  9. # make a frame. Note that the widget is 200 x 200
  10. # and the window containing is 400x400. We do this
  11. # simply to show that this is possible. The rest of the
  12. # area is inaccesssible.
  13. f = Frame(top, width=200, height=200, background='green')
  14. # note that we use a different manager here.
  15. # This way, the top level frame widget resizes when the
  16. # application window does.
  17. f.pack(fill=BOTH, expand=1)
  18. # now make a button
  19. f.button = Button(f, foreground='red', text='amazing', command=dothis)
  20. # and place it so that the nw corner is
  21. # 1/2 way along the top X edge of its' parent
  22. f.button.place(relx=0.5, rely=0.0, anchor=NW)
  23. # allow the user to move the button SUIT-style.
  24. f.bind('<Control-Shift-Motion>', do_motion)
  25. return f
  26. root = Tk()
  27. app = createWidgets(root)
  28. root.geometry("400x400")
  29. root.maxsize(1000, 1000)
  30. root.mainloop()