【问题标题】:How to draw a perfect square using tkinter canvas?如何使用 tkinter 画布绘制一个完美的正方形?
【发布时间】:2020-08-08 10:35:55
【问题描述】:
I want to create a square on mouse drag. I can only create rectangles with 

canvas.create_rectangle(oldX,oldY , newX , newY,fill = 'red',outline = "")

有什么方法可以只创建正方形吗?

【问题讨论】:

  • 正方形就是边长相等的长方形。

标签: python python-3.x tkinter canvas tkinter-canvas


【解决方案1】:

您可以使用宽度和高度的最大值或最小值作为正方形的大小:

dx, dy = newX-oldX, newY-oldY
signx = 1 if dx >= 0 else -1
signy = 1 if dy >= 0 else -1
# user either min() or max() in below statement
size = max(abs(dx), abs(dy))
canvas.create_rectangle(oldX, oldY, oldX+size*signx, oldY+size*signy, fill='red', outline='', tag='rect')

【讨论】:

    猜你喜欢
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多