简体   繁体   English

有使用tkinter创建许多多边形的捷径吗?

[英]Is there a shortcut for creating many polygons with tkinter?

I need to create a grid of triangles like in this picture: https://www.tilelook.com/system/tile_picture/resource/4973584/d3d_default_RE04MC017.png . 我需要创建一个如以下图片所示的三角形网格: https : //www.tilelook.com/system/tile_picture/resource/4973584/d3d_default_RE04MC017.png

As i want the triangles to be clickable I'm using canvas function create_polygon for drawing and bind function for listening to the click event. 因为我希望三角形是可单击的,所以我使用画布函数create_polygon进行绘制,并使用bind函数来侦听click事件。 The problem is that i need to draw many triangles and this would require to calculate by hand the vertices of each triangle. 问题是我需要绘制许多三角形,这将需要手工计算每个三角形的顶点。

Is there a faster method like drawing some parallel and intersecting lines and tell tkinter that the vertices are the intersections of the lines or something that doesn't involve calculating the vertices of each triangle? 有没有更快的方法,例如绘制一些平行和相交的线并告诉tkinter顶点是这些线的交点或不涉及计算每个三角形的顶点的东西?

While you can't get the intersections from within tkinter you could instead make triangles with a couple for loops and then plot these on the canvas. 虽然您无法从tkinter内获取交点,但可以改为使用一对for循环制作三角形,然后将其绘制在画布上。 An example would look like: 一个例子如下:

import tkinter as tk
import random

def make_triangles(row_height=60, tri_width=60, max_height=1800, max_width=1800):
    triangle_list = []
    half_width = int(tri_width/2)
    for i in range(0, max_height, row_height):
        for j in range(0, max_width, half_width):
            if j % tri_width == 0:
                triangle = (i, j-half_width, i+row_height, j, i, j+half_width)
            else:
                triangle = (i, j, i+row_height, j+half_width, i+row_height, j-half_width)
            triangle_list.append(triangle)
    return triangle_list

win = tk.Tk()
canv = tk.Canvas(win)
triangles = make_triangles()
for tri in triangles:
    canv.create_polygon(tri, fill=random.choice(["blue", "red", "green", "brown", "yellow", "black"]))
canv.pack()
win.mainloop()

Where we just generate many triangles and then plot them successively onto the canvas (we don't have to find each vertex by hand, just a formula that would describe them all). 在这里,我们只是生成许多三角形,然后将它们依次绘制到画布上(我们不必手动找到每个顶点,只需一个描述所有顶点的公式即可)。 While this doesn't solve your exact problem in that the triangles don't intersect (you would have to shift every other row by half a triangle), and the triangles are left/right instead of up/down, it does answer your question " Is there a shortcut for creating many polygons with tkinter "! 虽然这不能解决您的确切问题,因为三角形不相交(您必须每隔一行将三角形移动一半),并且三角形向左/向右而不是向上/向下,但确实可以回答您的问题“ 是否有使用tkinter创建许多多边形的捷径 ”! The answer is to just make a list of your polygons and then plot them all. 答案是只列出多边形,然后将它们全部绘制出来。 A sample output (yours will be different because it is random) of my code looks like: 我的代码的示例输出(您的会有所不同,因为它是随机的)看起来像:

产生的三角形

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM