我对 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/