可以改变乌龟的笔触吗? [英] Is it possible to change turtle's pen stroke?
问题描述
我需要使用Python的turtle图形绘制条形图,我认为简单地将钢笔制成粗正方形会更容易,这样我就可以绘制这样的条形,而不必担心制作数十个矩形并填充它们内.
但是当我使用turtle.shape('square')
设置乌龟形状时,它只会改变笔的外观,但对实际图形没有影响:
是否有一种方法可以使乌龟实际绘制矩形笔画,无论是通过内置方法还是通过修改乌龟文件?
我不要像这样的圆形边缘:
要回答标题中的问题:否,无法直接更改笔触 (请参阅 cdlane的答案,其中一种可能的方法是通过修改tkinter
)中的硬编码值来实现.>
但是,我确实在问题正文中找到了针对该用例的解决方法.
可以按如下所示注册自定义笔形(在这种情况下,代表条的确切形状和大小):
screen.register_shape("bar", ((width / 2, 0), (-width / 2, 0), (-width / 2, height), (width / 2, height)))`
然后,我们可以简单地循环浏览每个条形图,用新值更新笔形,然后使用turtle.stamp
将完成的条形图标记到图形上,而无需绘制.
I need to draw a bar graph using Python's turtle graphics and I figured it would be easier to simply make the pen a thick square so I could draw the bars like that and not have to worry about making dozens of rectangles and filling them in.
When I set the turtle shape using turtle.shape('square')
though, it only changes the appearance of the pen but has no effect on the actual drawing:
Is there a way to make turtle actually draw a rectangular stroke, whether that be through built-in methods or through modifying the turtle file?
I DON'T want rounded edges, like this:
To answer the question asked in the title: No, it is not possible to change the pen stroke directly (see cdlane's answer for a possible way to do it by modifying the hardcoded values from tkinter
).
I did find a workaround for the use case presented in the question body, however.
A custom pen shape (in this case, representing the exact shape and size of the bar) can be registered like this:
screen.register_shape("bar", ((width / 2, 0), (-width / 2, 0), (-width / 2, height), (width / 2, height)))`
We can then simply loop through each bar, update the pen shape with the new values, and use turtle.stamp
to stamp the completed bars onto the graph, no drawing required.
这篇关于可以改变乌龟的笔触吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!