Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import * # Import tkinter
- class CanvasDemo:
- def __init__(self):
- window = Tk() # Create a window
- window.title("Canvas Demo") # Set title
- # Place self.canvas in the window
- self.canvas = Canvas(window, width = 500, height = 200,
- bg = "white")
- self.canvas.pack()
- # Place buttons in frame
- frame = Frame(window)
- frame.pack()
- btBigger = Button(frame, text = "Bigger",
- command = self.increaseCircle)
- btSmaller = Button(frame, text = "Smaller",
- command = self.decreaseCircle)
- btClear = Button(frame, text = "Clear",
- command = self.clearCanvas)
- btBigger.grid(row = 1, column = 1)
- btSmaller.grid(row = 1, column = 2)
- btClear.grid(row = 1, column = 7)
- window.mainloop() # Create an event loop
- def increaseCircle(self):
- self.canvas.delete("oval")
- global radius
- if radius < 100:
- radius += 2
- self.canvas.create_oval(100 - radius, 100 - radius,
- 100 + radius, 100 + radius, tags = "oval")
- def decreaseCircle(self):
- self.canvas.delete("oval")
- global radius
- if radius > 2:
- radius -= 2
- self.canvas.create_oval(100 - radius, 100 - radius,
- 100 + radius, 100 + radius, tags = "oval")
- self.canvas = Canvas(window, bg = "white", width = 200, height = 200)
- canvas.pack()
- radius = 50
- self.canvas.create_oval(100 - radius, 100 - radius,
- 100 + radius, 100 + radius, tags = "oval")
- # Bind canvas with mouse events
- self.canvas.bind(btBigger, increaseCircle)
- self.canvas.bind(btSmaller, decreaseCircle)
- # Clear drawings
- def clearCanvas(self):
- self.canvas.delete("oval")
- CanvasDemo() # Create GUI
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement