Cannot get colors right in the function

喜你入骨 提交于 2019-12-25 03:23:06

问题


from turtle import *

reset()
setworldcoordinates(0, 500, 600, 0)

def rectangle(a, b, c, d, pen_color, fill_color, pensize):
    up()
    setpos (a,b)
    down()
    fd (c-a)
    left(90)
    fd(d-b)
    left (90)
    fd (c-a)
    left (90)
    fd (d-b)
    left(90)

    p = pen_color.strip("pen_color=")
    pen_color(p)
    s = fill_color.strip("fill_color=")
    fill_color(värv2)
    a = pensize.strip("pensize=")
    a = int(a) 
    pensize(a)


rectangle(10,10,200,100, pen_color="red", fill_color="blue", pensize=3)

Problem is that I cannot get those colors in the last line. If I start the program, it won't make the pen_color red and fill_color blue and pensize 3. It stays as normal. Any idea how to get those colors right? How to make that function to read those colors from that last line right? Thank you in advance.


回答1:


In the first two calls to strip remove the string arguments and remove the whole line with the last strip:

p = pen_color.strip()
pen_color(p)
s = fill_color.strip()
fill_color(s)
a = int(a) 
pensize(a)

This code block has to be before the real drawing is done, direct after the def rectangle...




回答2:


This question seems to come down to two problems: a misunderstanding on the OP's part that when you pass named arguments that somehow you had to strip off the name; a design error on the OP's part of wild card importing turtle and then using variables that matched turtle function names and/or incorrect turtle method names. The revised code:

from turtle import *

setworldcoordinates(0, 500, 600, 0)

def rectangle(a, b, c, d, pen_color, fill_color, pen_size):
    pencolor(pen_color)
    fillcolor(fill_color)
    pensize(pen_size)

    up()
    setpos(a, b)
    down()

    begin_fill()
    fd(c - a)
    left(90)
    fd(d - b)
    left(90)
    fd(c - a)
    left(90)
    fd(d - b)
    end_fill()

    left(90)

rectangle(10, 10, 200, 100, pen_color="red", fill_color="blue", pen_size=3)

mainloop()


来源:https://stackoverflow.com/questions/12848062/cannot-get-colors-right-in-the-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!