dimanche 8 mars 2015

If condition Python being ignored?

I am learning about tkinter & objects with Python 2.7 and am drawing lines from a given point (x0,y0) to a point chosen by a mouse click (x1,y1).


I am trying to keep the line an equal length under certain conditions (see code) and just point in the direction of the mouse selection.


The code,



from Tkinter import *
import numpy as np

class App:

def __init__(self, master):

frame = Frame(master)
frame.pack()

self.can = Canvas(master, width=800, height=800)
self.can.configure(cursor="crosshair")
self.can.pack()
self.start_point = [400, 400]
self.end_point = [250, 400]
self.line = self.can.create_line(self.start_point, self.end_point)
self.can.bind("<Button-1>", self.line_end)
self.can.bind("<Button-3>", self.del_line)

def line_end(self, event):
r = 150
x0 = self.start_point[0]
y0 = self.start_point[1]
x1 = event.x
y1 = event.y
p = x1
q = y1
if x1 > x0 and y1 > y0:
theta = np.arctan((y1-y0)/(x1-x0))
p = int(x0 + r*np.cos(theta))
q = int(y0 + r*np.sin(theta))
print 'A'
if x1 < x0 and y1 > y0:
theta = np.arctan((y1-y0)/(x0-x1))
p = int(x0 + r*np.cos(theta))
q = int(y0 + r*np.sin(theta))
print 'S'
self.can.delete(self.line)
self.line = self.can.create_line(x0, y0, p, q)
print x0, y0, ' ........ ',x1, y1

def del_line(self, event):
self.can.delete(self.line)

root = Tk()
app = App(root)
root.mainloop()


When there definitely cases where the conditionals are met they are not entered. Am I using Pythons if structures correctly?


Aucun commentaire:

Enregistrer un commentaire