单击鼠标绘制矩形[Python] [英] Draw rectangle on mouse click [Python]

查看:412
本文介绍了单击鼠标绘制矩形[Python]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def xaxis(event):
   x1, y1 = (event.x - 1), (event.y - 1)

def yaxis(event):
   x2, y2 = (event.x + 1), (event.y + 1)

def create(event):
   w.create_rectangle(x1,y1,x2,y2,fill='Black')

w = Canvas(root, width=canvas_width, height=canvas_height)
w.config(cursor='cross')
w.pack(expand=YES, fill=BOTH)
w.bind("<Button-1>", xaxis)
w.bind("<ButtonRelease-1>", yaxis)
w.bind("<ButtonRelease-1>", create)

shell表示


Tkinter回调Traceback中的异常(最近一次调用为最新):

文件
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py,
第1410行,在调用
返回中self.func(* args)文件 /Users/Leo/Desktop/draw.py,第22行,在create
w.create_rectangle(x1,y1,x2,y2,fill ='Black')中出现NameError:全局名称 x1未定义

Exception in Tkinter callback Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1410, in call return self.func(*args) File "/Users/Leo/Desktop/draw.py", line 22, in create w.create_rectangle(x1,y1,x2,y2,fill='Black') NameError: global name 'x1' is not defined

,它认为create函数无法获取其他函数的坐标...

and it think the create function is not able to get the coordinates of the other functions...

我这样做是因为以后需要坐标!

I did it this way because I need the coordinates later!

希望您能帮帮我.. ;-)
谢谢!

I hope you can help me.. ;-) Thanks!

推荐答案

解决未定义全局名称'x1'问题



调试时的一个很好的经验法则是假设错误消息是在讲真话。在这种情况下,就是说没有名为 x1的全局变量。所以问自己为什么?。要么根本不创建 x1变量,要么创建它的方式不是全局变量。

Solving the "global name 'x1' is not defined" problem

A good rule of thumb when debugging is to assume the error message is telling the truth. In this case it is saying that there is no global variable named "x1". So ask youself "why?". Either you aren't creating an "x1" variable at all, or you're creating it in such a way that it's not global.

在您的情况下,当您定义x1,y1,x2和y2,则将它们创建为局部变量。创建变量时,这是python的默认行为。最简单的解决方案是将它们声明为全局变量:

In your case, when you define x1, y1, x2 and y2, you are creating them as local variables. This is python's default behavior when creating variables. The simplest solution is to declare them as global:

def xaxis(event):
   global x1, y1
   x1, y1 = (event.x - 1), (event.y - 1)

def yaxis(event):
    global x2, y2
    x2, y2 = (event.x + 1), (event.y + 1)

不相关的第二个问题

您的代码在处理绑定方面还有另一个问题。考虑以下代码片段:

You have another problem in your code with how you do your bindings. Consider this code snippet:

w.bind("<ButtonRelease-1>", yaxis)
w.bind("<ButtonRelease-1>", create)

您没有创建两个绑定,您正在创建一个,然后用另一个覆盖它。但是,您不需要两个绑定。您可以从create函数中调用yaxis函数。

You aren't creating two bindings, you are creating one, and then overwriting it with another. You don't need two bindings, however. You can call your yaxis function from within the create function.

但是,全局变量通常不是解决问题的最佳方法。如果切换为使用面向对象的方法,则可以将坐标存储为对象的属性。这是一个完整的工作示例:

However, global variables aren't usually the best solution to a problem. If you switched to using an object-oriented approach then you can store the coordinates as attributes of an object. Here's a complete, working example:

import Tkinter as tk

class ExampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.x = self.y = 0
        self.canvas = tk.Canvas(self, width=400, height=400, cursor="cross")
        self.canvas.pack(side="top", fill="both", expand=True)
        self.canvas.bind("<ButtonPress-1>", self.on_button_press)
        self.canvas.bind("<ButtonRelease-1>", self.on_button_release)

    def on_button_press(self, event):
        self.x = event.x
        self.y = event.y

    def on_button_release(self, event):
        x0,y0 = (self.x, self.y)
        x1,y1 = (event.x, event.y)

        self.canvas.create_rectangle(x0,y0,x1,y1, fill="black")

if __name__ == "__main__":
    app = ExampleApp()
    app.mainloop()

拖动鼠标时绘制

如果要在拖动光标时绘制矩形,可以更改程序以在按下按钮时创建矩形。如果您为对象提供唯一的标签或保存画布id ,您可以设置鼠标移动事件以使用 coords 方法。我将其留给读者练习,因为它与所提出的问题没有直接关系。

If you want to draw the rectangle as you drag the cursor, you can alter the program to create the rectangle on the button press. If you either give the object a unique tag or save the canvas id, you can set up a mouse motion event to adjust the coordinates of the current rectangle using the coords method of the canvas object. I'll leave that as an exercise for the reader since it isn't directly related to the question that was asked.

这篇关于单击鼠标绘制矩形[Python]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆