Draw a rectangle on mouse click [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)

The shell says

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

and he believes that the create function cannot get the coordinates of other functions ...

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

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

+1
3

" " x1 "

, , . , "x1". "?". "x1", , .

, x1, y1, x2 y2, . python . - :

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)

, . :

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

, , . . yaxis create.

-

. - , . :

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 canvas. , .

+8

x1 .. (, xaxis), create. , :

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

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

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

You save the results x1, x2 ... in local variables. These values ​​are lost when you exit the event handler function. You must use global vars or a dedicated object to control event handlers. I would prefer a second solution.

Another problem is that you have two functions related to Button-Release1. My solution is not to bind yaxis and call it creation. A cleaner approach can be found :)

See the example below. It should work:

from Tkinter import *

root=Tk()

class Handler:

    def __init__(self, w):
        self.w = w
        w.bind("<Button-1>", self.xaxis)
        #w.bind("<ButtonRelease-1>", self.yaxis)
        w.bind("<ButtonRelease-1>", self.create)


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

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

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

w = Canvas(root, width=200, height=200)
w.config(cursor='cross')
w.pack(expand=YES, fill=BOTH)

Handler(w)


root.mainloop()
0
source

Source: https://habr.com/ru/post/1543958/


All Articles