Below is the code to draw a rectangle by click drag and release... For some reason its not working, the canvas screen is showing up but the rectangle isn't being drawn? Is it the root.mainloop() line? Because I changed it because I needed to draw arcs and lines and couldn't just have app = rectangle and app.mainloop... Sorry I'm really new to this.
from tkinter import Canvas, Tk, mainloop
import tkinter as tk
from PIL import Image, ImageTk
# Image dimensions
w,h = 800,400
# Create canvas
root = Tk()
canvas = Canvas(root, width = w, height = h, bg='#D2B48C', cursor = "cross")
canvas.pack()
class Rectangle(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
self.canvas.pack(side="top", fill = "both", expand = True)
self.canvas.bind("<ButtonPress-1>", self.press)
self.canvas.bind("<B1-Motion>", self.move)
self.canvas.bind("<ButtonRelease-1>", self.release)
self.rect = None
self.start_x = None
self.start_y = None
def press(self, event):
# save mouse drag start position
self.start_x = event.x
self.start_y = event.y
self.rect = self.canvas.create_rectangle(self.x, self.y, 1, 1, fill="red")
def move(self, event):
mouseX, mouseY = (event.x, event.y)
# expand rectangle as you drag the mouse
self.canvas.coords(self.rect, self.start_x, self.start_y, mouseX, mouseY)
def release(self, event):
pass
# Other Classes for arc and pencil begin here
root.mainloop()
Thank you all!!!