We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

user drawing program

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.

2
Contributors
3
Replies
21 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
Thropian
Junior Poster
102 posts since Oct 2010
Reputation Points: 13
Solved Threads: 2
Skill Endorsements: 0

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.

pyTony
pyMod
Moderator
6,299 posts since Apr 2010
Reputation Points: 879
Solved Threads: 984
Skill Endorsements: 26

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.

Thropian
Junior Poster
102 posts since Oct 2010
Reputation Points: 13
Solved Threads: 2
Skill Endorsements: 0

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.

Thropian
Junior Poster
102 posts since Oct 2010
Reputation Points: 13
Solved Threads: 2
Skill Endorsements: 0
Question Answered as of 1 Year Ago by pyTony

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0661 seconds using 2.71MB