MissSprezzatura
MissSprezzatura

Reputation: 313

draw rectangle tkinter not functioning

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!!!

Upvotes: 1

Views: 1617

Answers (2)

J. Piquard
J. Piquard

Reputation: 1663

As @BryanOakley comments, the provided source contains uncompleted analysis and reveals misunderstandings of tkinter use.

Problem 1 - the class Rectangle doesn't need to inherit from tk.Tk.

Instead of inheriting the class Rectangle from tk.Tk, add the Canvas instance at instance creation.

 class Rectangle(): # not inherit from ==> tk.Tk):
    def __init__(self,canvas):
        # not initialize a second Tk instance ==> tk.Tk.__init__(self)
        self.x = self.y = 0
        #  attach the main canvas
        self.canvas = canvas
        self.canvas.pack(side="top", fill = "both", expand = True)

Instead of:

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)

Problem 2 - no instance of the class Rectangle has been created.

To use the class Rectangle, an instance shall be created and attached to the canvas instance !!

Before, move the class declaration before Tk() and Canvas() creation.

class Rectangle(tk.Tk):
    def __init__(self):
...
    def release(self, event):
        pass

# Other Classes for arc and pencil begin here

root = Tk()
canvas = Canvas(root, width = w, height = h, bg='#D2B48C', cursor = "cross")
canvas.pack()

# create the Rectangle instance
hRect = Rectangle(canvas)

root.mainloop()

Instead of:

root = Tk()
canvas = Canvas(root, width = w, height = h, bg='#D2B48C', cursor = "cross")
canvas.pack()

class Rectangle(tk.Tk):
    def __init__(self):
...
    def release(self, event):
        pass

# Other Classes for arc and pencil begin here

root.mainloop()

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 385970

For one, you never create an instance of the Rectangle class which is where all your logic is.

For another, you must only have a single instance of Tk. You create one at the start of the problem, and then you'll create another if you create an instance of Rectangle. Again, you can only have one.

Upvotes: 0

Related Questions