gpt4 book ai didi

python - 如何在 Canvas 上画线?

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

我在互联网上阅读了一些教程,但我似乎找不到任何教我如何画线的东西

有人能帮忙吗?

我试过

p = Canvas(height = 600, width = 800).place(x=0,y=0)
p.create_rectangle(50, 25, 150, 75, fill="blue")

不幸的是,它没有用。

最佳答案

不完全确定您在问什么,因为您既没有向我们展示您的完整代码,也没有说明究竟是什么“不起作用”。看来你已经找到了如何绘制一个矩形,同样的教程应该也有关于绘制线条的内容,比如 the one linked in the comments .

因为这似乎对您没有帮助,也许问题在于您使用的是 Python 3,其中 Tkinter 包已重命名为 tkinter。这个例子应该适合你:

import tkinter

root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.pack()

for i in range(10):
canvas.create_line(50 * i, 0, 50 * i, 400)
canvas.create_line(0, 50 * i, 400, 50 * i)
canvas.create_rectangle(100, 100, 200, 200, fill="blue")
canvas.create_line(50, 100, 250, 200, fill="red", width=10)

root.mainloop()

附录:我刚刚注意到您的代码存在两个实际问题:

  • 通过执行 p = Canvas(height = 600, width = 800).place(x=0,y=0),变量 p不是被分配Canvas,而是place的返回值,即None
  • 此外,构造函数应包括要将 Canvas 添加到的父元素(在我的示例中为 root)。

这是一个very thorough introduction to all of Tkinter ,尤其是 Canvas 元素。

关于python - 如何在 Canvas 上画线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25701347/

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