I started making a simple drawing program, to use for a level maker, and I came across a slight issue

from Tkinter import *

class Main:
    def __init__(self,root):
        w,h = root.winfo_screenwidth(), root.winfo_screenheight()
        current = {}

        canvas = Canvas(root, width = w, height = h, bd=0)
        canvas.pack()

        root.overrideredirect(1)
        root.geometry("%dx%d+0+0" % (w, h))

        def save(event):#save all tiles to file for use. ## not implemented yet
            for x in canvas.find_all():
                print canvas.coords(x)
            root.destroy()

        def Start(event):#start drawing
            current["x"],current["y"] = event.x,event.y

        def Stretch(event):#change second coords to stretch tile
            for x in canvas.find_withtag("current"):
                canvas.delete(x)
            canvas.create_rectangle(current["x"],current["y"],event.x,event.y,tags="current")

        def Stop(event):#end drawing
            for x in canvas.find_withtag("current"):
                canvas.delete(x)
            canvas.create_rectangle(current["x"],current["y"],event.x,event.y)

        menu = Canvas(canvas, width = 100, height = 50, bg="Blue")
        menu.bind("<Button-1>",save)#so you don't have to alt-f4
        menu.place(x = w/2-50, y = h/2-25)
        canvas.bind("<Button-1>",Start)
        canvas.bind("<B1-Motion>",Stretch)#all important bindings
        canvas.bind("<ButtonRelease-1>",Stop)

root = Tk()
Main(root)
root.wait_window()

If you try to start a new rectangle on the edge of an existing rectangle, it deletes the existing one.
You can end on an existing rectangle, or just leave them free floating. Which is really confusing me.

Any help would be appreciated.

Recommended Answers

All 3 Replies

Why you are deleting instead of changing the size of rectangle? I do not see you changing the current tag anywhere, so all shapes will have it set.

I had issues where if I just changed the side, I would start getting several rectangles. This would only be noticable if I dragged really fast and then let go.
And on the Stop section, it creates a rectangle with no tags.

So the issue was something to do with the tags. having removed the tags I was able to get the rectangles to stay where they belonged.

from Tkinter import *

class Main:
    def __init__(self,root):
        w,h = root.winfo_screenwidth(), root.winfo_screenheight()
        current = {}

        canvas = Canvas(root, width = w, height = h, bd=0)
        canvas.pack()

        root.overrideredirect(1)
        root.geometry("%dx%d+0+0" % (w, h))

        def save(event):#save all tiles to file for use. ## not implemented yet
            for x in canvas.find_all():
                print canvas.coords(x)
            root.destroy()

        def Start(event):#start drawing
            current["x"],current["y"] = event.x,event.y
            shape = canvas.create_rectangle(current["x"],current["y"],event.x,event.y)
            current["shape"] = shape

        def Stretch(event):#change second coords to stretch tile
            canvas.coords(current["shape"],current["x"],current["y"],event.x,event.y)


        menu = Canvas(canvas, width = 100, height = 50, bg="Blue")
        menu.bind("<Button-1>",save)#so you don't have to alt-f4
        menu.place(x = w/2-50, y = h/2-25)
        canvas.bind("<Button-1>",Start)
        canvas.bind("<B1-Motion>",Stretch)#all important bindings

root = Tk()
Main(root)
root.wait_window()

I was also able to remove the stop function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.