如何编写一个程序,将用户的输入作为三个鼠标单击的坐标,然后使用这三个点在图形窗口中绘制一个三角形?用于此目的的图形模块是什么?
答案 0 :(得分:0)
每次单击画布三次,都会绘制一个三角形。
from tkinter import *
#to store triangle coordinates
coordinates = []
root = Tk()
#called everytime the canvas is clicked
def pressed(event):
global coordinates
coordinates.append((event.x, event.y))
if len(coordinates) == 3:
for x in range(3):
secondCordinate = (x + 1) % 3
canvas.create_line(coordinates[x][0], coordinates[x][1], coordinates[secondCordinate][0], coordinates[secondCordinate][1])
coordinates = []
canvas= Canvas(root, width=400, height=400)
canvas.bind("<Button-1>", pressed)
canvas.pack()
root.mainloop()