Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
颜色不变-python图形_Python - Fatal编程技术网

颜色不变-python图形

颜色不变-python图形,python,Python,我正试图复制这样的东西 import graphics from graphics import color_rgb import random window= graphics.GraphWin("x", 600, 400) stripes = input("How many stripes should be on the flag") stripes = int(stripes) count = 0 count = int(count) P1=graphics.Point(0,0) #le

我正试图复制这样的东西

import graphics
from graphics import color_rgb
import random
window= graphics.GraphWin("x", 600, 400)
stripes = input("How many stripes should be on the flag")
stripes = int(stripes)
count = 0
count = int(count)
P1=graphics.Point(0,0) #left corner - anchor point
for x in range(stripes): #loop for number of stripes
    col= random.randint(1,255)
    stepdim = 400/stripes #size of divisions
    stepdim = int(stepdim)
    shrink = count*stepdim
    shrink = int(shrink)
    stepdim = stepdim*10 #enlarge to an increment below the last
    stepdim = stepdim-shrink
    stepdim = int(stepdim)
    P2=graphics.Point(600,stepdim) #bottom right corner - ever shrinking
    outsiderec=graphics.Rectangle(P1,P2) #
    outsiderec.setFill(color_rgb(100, col, 0))
    outsiderec.draw(window)
    count= count + 1
    count= int(count)
window.getMouse()
window.close()

我收到的是一张单色的。
我假设问题在我的rand(int)中。我真的不知道它的来龙去脉。它不是运行了不止一次吗?

以您的代码为基础,我试图重现预期的结果

import graphics
from graphics import color_rgb
import random

window= graphics.GraphWin("x", 600, 400)
stripes = input("How many stripes should be on the flag")
stripes = int(stripes)
#count = 0
#count = int(count)
#P1=graphics.Point(0,0) #left corner - anchor point
stepdim = 400/stripes #size of divisions
for x in range(stripes): #loop for number of stripes
    #col= random.randint(1,255)
    #stepdim = int(stepdim)
    #shrink = count*stepdim
    #shrink = int(shrink)
    #stepdim = stepdim*10 #enlarge to an increment below the last
    #stepdim = stepdim-shrink
    #stepdim = int(stepdim)
    #P2=graphics.Point(600,stepdim) #bottom right corner - ever shrinking
    P1=graphics.Point(0, stepdim * x) #left corner - anchor point
    P2=graphics.Point(600,stepdim * (x + 1)) #bottom right corner - ever shrinking
    outsiderec=graphics.Rectangle(P1,P2)
    #outsiderec.setFill(color_rgb(100, col, 0))
    red = random.randint(1, 255)
    green = random.randint(1, 255)
    blue = random.randint(1, 255)
    outsiderec.setFill(color_rgb(red, green, blue))
    outsiderec.draw(window)
    #count= count + 1
    #count= int(count)
window.getMouse()
window.close()
我有:

  • 注释掉所有不需要的语句
  • stepdim=400/条带从循环内部移动到外部,因为不需要为每个循环计算相同的值
  • P1=graphics.点(0,0)
    移动到循环内,只需稍加修改即可指向条纹的左上角
  • 修改了
    P2=graphics.Point(600,stepdim)
    以指向条纹的右下角
  • 为三种颜色分量添加了随机计算
    red=random.randint(1255)
    green=random.randint(1255)
    ,和
    blue=random.randint(1255)
意见:

我改变了一点条纹的画法。修改后的版本只是在连续位置绘制固定大小的条纹,而不是为每个循环绘制收缩版本