gpt4 book ai didi

python - 绘制的渐变颜色总是统一的

转载 作者:太空宇宙 更新时间:2023-11-04 01:27:32 24 4
gpt4 key购买 nike

我试图通过使用非常幼稚的方法(绘制线条序列)编写自己的简单函数来绘制渐变(仅灰色),但实际上代表渐变的矩形的颜色始终是统一的颜色(我认为它是循环中的最新颜色)。你能解释一下为什么吗?这是代码:

import Tkinter

class testGUI:
def __init__( self, root ):
C = Tkinter.Canvas( root, bg = "blue", height = 250, width = 300 )
self.drawGradient( C, 10, 10, 100, 50 )
C.pack()

def drawGradient( self, canvas, x, y, w, h ):
for offset in range( 0, w ):
gradColor = '#%02x%02x%02x' % ( x * 10, x * 10, x * 10 )
canvas.create_line( x + offset, y, x + offset, y + h, fill = gradColor )

root = Tkinter.Tk()
app = testGUI( root )
root.mainloop()

最佳答案

颜色始终相同,因为您使用的颜色不依赖于循环的迭代:

for offset in ...
gradColor = '#%02x%02x%02x' % ( x * 10, x * 10, x * 10 )

要让它改变,gradColor的值必须依赖于offset的值,例如:

def drawGradient(self, canvas, x, y, w, h):
factor = 255./w
for offset in range(0, w):
gradColor = '#%02x%02x%02x' % (offset*factor, offset*factor, offset*factor)
canvas.create_line(x + offset, y, x + offset, y + h, fill=gradColor)

关于python - 绘制的渐变颜色总是统一的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16780174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com