我试图使用for循环在tkinter中绘制不同的颜色线。

时间:2014-09-22 16:14:30

标签: python

可以看出我是初学者,但尝试过一些事情,比如#code

from tkinter import *
def main():
    canvas = Canvas(width=300, height=300, bg='white')
    canvas.pack(expand=YES, fill=BOTH)        
    #color=["red", "blue", "green"]
    color=(0, 255, 65535)
    x0,y0=150,150
    for i in range (1,4):

            #y=color(i-1)

            x1,y1=x0+i*10+10,y0-i*10+10
            canvas.create_line(0,0,x0,y0)
            canvas.create_line(x0,y0,x1,y1,fill=color(i))
            #canvas.create_line(x0,y0,x1,y1,fill="red")

main()

1 个答案:

答案 0 :(得分:-1)

from tkinter import *
def main():
    canvas = Canvas(width=300, height=300, bg='white')
    canvas.pack(expand=YES, fill=BOTH)        
    color=["red", "blue", "green","yellow","magenta"]

    x0,y0=150,150

    for i in range (5):
            m=(-1)**i
            print(m)

            x1,y1=x0+(m)*i*20+20,y0-(m)*i*10
            print(x1,y1)
            print(color[i])
            canvas.create_line(0,0,x0,y0)
            canvas.create_line(x0,y0,x1,y1,fill=color[i])


main()