Advertisement
Guest User

Untitled

a guest
May 5th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. from tkinter import * # Import tkinter
  2.  
  3. class CanvasDemo:
  4. def __init__(self):
  5. window = Tk() # Create a window
  6. window.title("Canvas Demo") # Set title
  7.  
  8. # Place self.canvas in the window
  9. self.canvas = Canvas(window, width = 500, height = 200,
  10. bg = "white")
  11. self.canvas.pack()
  12.  
  13. # Place buttons in frame
  14. frame = Frame(window)
  15. frame.pack()
  16. btBigger = Button(frame, text = "Bigger",
  17. command = self.increaseCircle)
  18. btSmaller = Button(frame, text = "Smaller",
  19. command = self.decreaseCircle)
  20.  
  21. btClear = Button(frame, text = "Clear",
  22. command = self.clearCanvas)
  23. btBigger.grid(row = 1, column = 1)
  24. btSmaller.grid(row = 1, column = 2)
  25. btClear.grid(row = 1, column = 7)
  26.  
  27. window.mainloop() # Create an event loop
  28.  
  29. def increaseCircle(self):
  30. self.canvas.delete("oval")
  31. global radius
  32. if radius < 100:
  33. radius += 2
  34. self.canvas.create_oval(100 - radius, 100 - radius,
  35. 100 + radius, 100 + radius, tags = "oval")
  36.  
  37. def decreaseCircle(self):
  38. self.canvas.delete("oval")
  39. global radius
  40. if radius > 2:
  41. radius -= 2
  42. self.canvas.create_oval(100 - radius, 100 - radius,
  43. 100 + radius, 100 + radius, tags = "oval")
  44.  
  45. self.canvas = Canvas(window, bg = "white", width = 200, height = 200)
  46. canvas.pack()
  47. radius = 50
  48. self.canvas.create_oval(100 - radius, 100 - radius,
  49. 100 + radius, 100 + radius, tags = "oval")
  50.  
  51. # Bind canvas with mouse events
  52. self.canvas.bind(btBigger, increaseCircle)
  53. self.canvas.bind(btSmaller, decreaseCircle)
  54.  
  55. # Clear drawings
  56. def clearCanvas(self):
  57. self.canvas.delete("oval")
  58.  
  59. CanvasDemo() # Create GUI
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement