0
votes

I have create a frame but do not know how to draw the geometry on the sub-frame.

Here is the code of my current window:

class App:
    def __init__(self, master):
        frame = Frame(master)
        frame.grid()

        self.Quit = Button(frame, text = "QUIT", command = frame.quit)
        self.Quit.grid(row = 0, column = 48, sticky = N)

        self.adpt = Button(frame, text = "Add Point", command = self.adpt)
        self.adpt.grid(row = 0, column = 49, sticky = N)

        self.adln = Button(frame, text = "Add Line", command = self.adln)
        self.adln.grid(row = 0, column = 50, sticky = N)

        self.adpg = Button(frame, text = "Add Polygon", command = self.adpg)
        self.adpg.grid(row = 0, column = 51, stick = N)


        iframe = Frame(frame, bd = 2, relief = RAISED,  width=1000, height=500)
        iframe.grid(row = 1, columnspan = 100, sticky = N)

    def adpt(self):
        pass

    def adln(self):
        pass

    def adpg(self):
        pass

I need to create each kind of geometry by clicking the corresponding button, and then draw it on the sub-frame, but I do not know how to use event to draw geometry in the sub-frame (iframe). For example, to draw point, click the button "Add point". Then Click on the sub-frame to generate a point. Double click the sub-frame to save the points to a point list.

The first problem is how to draw the point on the sub-frame by click on it.

The second problem is how to make the sub-frame handle double click and click separately. When I double click a widget, it first go through the click event, and then the double click event.

I have create classes to draw geometry with canvas. The class of point, line, polygon can draw geometry with canvas.

Here is the example codes for point class:

class Point:
    def __init__(self,x, y):
        self.x = x
        self.y = y
    def __str__(self):
        return " (" + str(self.x) + "," + str(self.y) + ")"
    def draw(self,canvas):
        canvas.create_line(self.x-10,self.y,self.x+10,self.y)
        canvas.create_line(self.x,self.y-10,self.x,self.y+10)
1

1 Answers

0
votes

If you are selecting the type of geometry with the buttons, on their event handlers you can set the class that is going to be used. Then you can use the coordinates of the event information to draw the item on the canvas.

self.adln = Button(frame, text = "Add Line", command=self.adln)
self.adpt = Button(frame, text = "Add Point", command=self.adpt)
self.canvas.bind("<Button-1>", self.click)

#...
def adln(self):
    self.geometry = "Line"
def adpt(self):
    self.geometry = "Points"
#...

def click(self, event):
    if self.start is None:
        self.start = (event.x, event.y)
    else:
        self.draw_geometry()
        self.start = None
def draw_geometry(self):
    if self.geometry == "Points":
        p1 = Point(*self.start)
        p2 = Point(event.x, event.y)
        p1.draw(self.canvas)
        p2.draw(self.canvas)
    elif self.geometry == "Line":
        line = Line(event.x, event.y, *self.start)
        line.draw(self.canvas)

Note the number of arguments of the constructors and the existance of the draw method may not correspond with what you have, since this is just an example. It is a bit unpythonic, but it's the simplest way I have come out.

On the other hand, since the event <Button-1> is always triggered on a double click, I suggest you to use another button for a separate action, like <Button-2> or <Button-3>.