python - tkinter 多边形

标签 python python-3.x tkinter

我对 tkinter 比较陌生,我正在制作一个仅使用正方形的游戏。我正在抄的书只显示了三角形。这是代码:

# The tkinter launcher (Already complete)
from tkinter import *
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title ('VOID')
c = Canvas (window, width=WIDTH, height=HEIGHT, bg='black')
c.pack()
# Block maker (Issue)
ship_id = c.create_polygon (5, 5, 5, 25, 30, 15, fill='red')

我没有收到任何错误,它只是一串数字,(5, 5, 5, 25, 30, 15),我没有得到,因为我尝试制作一个正方形。

最佳答案

Canvas.create_polygon definition 的摘要:

As displayed, a polygon has two parts: its outline and its interior. Its geometry is specified as a series of vertices [(x0, y0), (x1, y1), … (xn, yn)] (...)

id = C.create_polygon(x0, y0, x1, y1, ..., option, ...)

所以你需要按照这个指定的顺序传递方 block 的坐标。 例如:

myCanvas.create_polygon(5, 5, 5, 10, 10, 10, 10, 5)

可以读作

myCanvas.create_polygon(5,5, 5,10, 10,10, 10,5)

将创建一个正方形,其顶点为 (5, 5)(5, 10)(10, 10)(10, 5).

关于python - tkinter 多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46082381/

相关文章:

python - 恢复和评估 Tensorflow 模型

python-3.x - 在将 tk PhotoImage 转换回 PIL 图像以保存时遇到一些问题

python - 使用python从html元标记中确定字符集

python - 有没有办法将 Python Shell 输出放在 tkinter 窗口中?

python - 获取 Tk winfo_rgb() 而不实例化窗口

python - 带有python请求库的zomato api请求

python - Pytest 根据 mark.parameterize 值选择测试?

python - Scrapy:创建爬行索引页面并保存每个对应链接的整个 HTML 页面的 Spider

python - Backspace\b 转义序列不适用于多个 print() 函数

python - Tkinter - 将键盘按键绑定(bind)到 Canvas 上的元素/项目