2015-03-08 55 views
-1

我正在学习使用Python 2.7的tkinter &对象,并从给定点(x0,y0)绘制线条到通过鼠标单击(x1,y1)选择的点。如果条件Python被忽略?

我想保持行在一定的条件下(见代码)相等的长度,只是指向鼠标选择的方向。

的代码,

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() 

当他们没有进入有一定情况下条件语句被满足。我是否使用Pythons 如果结构正确?

+1

你从不画垂直线,水平线或向下倾斜线吗? – stark 2015-03-08 18:23:27

+0

该代码将以任何方向绘制一条线。我只是想在某些方向上添加对线条长度的限制。 – DrBwts 2015-03-08 18:29:25

+0

我真的不知道为什么有人会投下这个票,这是一个合法的问题。 – DrBwts 2015-03-08 19:10:23

回答

0

行的解决方案是二折,

1)打开和运行在一个新的Python控制台解决了非条目到条件块。我不知道为什么,但它的工作。

2)将坐标转换为浮点,然后返回到int &考虑到斯塔克下面表达的不规则行为停止了(愚蠢的弧度!),下面修改了代码。

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 = float(self.start_point[0]) 
     y0 = float(self.start_point[1]) 
     x1 = float(event.x) 
     y1 = float(event.y) 
     p = int(x1) 
     q = int(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', p, q, theta 
     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', p, q, theta 
     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()