我花了一段时间,但你这是怎么画在你所希望的方式在画布:
import Tkinter as tk
root = tk.Tk()
root.geometry("500x500")
root.title("Drawing lines to a canvas")
cv = tk.Canvas(root,height="500",width="500",bg="white")
cv.pack()
def linemaker(screen_points):
""" Function to take list of points and make them into lines
"""
is_first = True
# Set up some variables to hold x,y coods
x0 = y0 = 0
# Grab each pair of points from the input list
for (x,y) in screen_points:
# If its the first point in a set, set x0,y0 to the values
if is_first:
x0 = x
y0 = y
is_first = False
else:
# If its not the fist point yeild previous pair and current pair
yield x0,y0,x,y
# Set current x,y to start coords of next line
x0,y0 = x,y
list_of_screen_coods = [(50,250),(150,100),(250,250),(350,100)]
for (x0,y0,x1,y1) in linemaker(list_of_screen_coods):
cv.create_line(x0,y0,x1,y1, width=1,fill="red")
root.mainloop()
您需要与x供应create_line,在该行的起点和终点y位置,在上面的示例代码(工程)中,我绘制了连接点(50,250),(150,100),(250,250),(350,100)的四条线,其曲线如下:
其值得指出的是x,y画布上的坐标从左上角开始,而不是左下角,可以认为它不像图表中的x,y = 0,0在画布的左下角,更多的是如何从顶部开始打印到页面乐在您向下移动页面时,在x中向右移动并且y递增。
我用: http://www.tutorialspoint.com/python/tk_canvas.htm作为参考。
你应该提到你正在使用Tkinter(我认为?) – 2013-05-11 07:36:46
是的,我拥有所有这些。这只是我整体代码的一部分。我编辑它。 – user2372332 2013-05-11 07:46:56
要使代码在本网站上正常显示,您需要[缩进4个空格](http://meta.stackexchange.com/a/22189/221039)。 – 2013-05-11 07:51:06