我创建了一个框架,但不知道如何在子框架上绘制几何体。
这是我当前窗口的代码:
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我需要通过单击相应的按钮创建每种几何体,然后在子框架上绘制它,但我不知道如何使用事件在子框架(iframe)中绘制几何体。 例如,要绘制点,请单击“添加点”按钮。 然后单击子框架以生成一个点。 双击子框架将点保存到点列表。
第一个问题是如何通过点击它来绘制子框架上的点。
第二个问题是如何使子框处理双击并单独单击。 当我双击一个小部件时,它首先通过click事件,然后是双击事件。
我创建了用canvas绘制几何的类。 点,线,多边形类可以使用画布绘制几何体。
以下是point类的示例代码:
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)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): passI 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)最满意答案
如果要使用按钮选择几何类型,则可以在其事件处理程序上设置要使用的类。 然后,您可以使用事件信息的坐标在画布上绘制项目。
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)请注意,构造函数的参数数量和draw方法的存在可能与您所拥有的不一致,因为这只是一个示例。 它有点unpythonic ,但它是我出来的最简单的方式。
另一方面,由于事件<Button-1>总是在双击时触发,我建议您使用另一个按钮进行单独操作,如<Button-2>或<Button-3> 。
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>.
更多推荐
发布评论