[英]How can I assign different points for shapes with tkinter?
I have tried to search for an answer to my question, but I have not find the solution to my problem, although my question seems to be fairly simple.我试图寻找我的问题的答案,但我还没有找到我的问题的解决方案,尽管我的问题似乎相当简单。 What I wish to know is how I can, for example, have 2 circles center points on the same location.
我想知道的是,例如,如何在同一位置有 2 个圆心点。 I am a beginner, so please don't be too harsh.
我是初学者,所以请不要太苛刻。
I don't quite know what information you have when trying to draw these circles, so will assume you know the centre and radius.我不太清楚在尝试绘制这些圆时您有什么信息,因此假设您知道中心和半径。 That's a very good starting point, however, as others have mentioned, tkinter requires the binding box of said circle.
这是一个非常好的起点,但是,正如其他人所提到的,tkinter 需要上述圆的绑定框。
I find everything easier with a diagram, so here is one:我发现使用图表更容易,所以这里有一个:
We know the coordinates of the circle's centre (marked "C") and also the radius (indicated by the two lines off "C" - both of length "r").我们知道圆心的坐标(标记为“C”)以及半径(由“C”外的两条线表示 - 长度均为“r”)。 The first thing I'm going to do is split the coordinates of "C" into different variables which I'll call x c and y c (for the x and y values respectively).
我要做的第一件事是将“C”的坐标拆分为不同的变量,我将它们称为 x c和 y c (分别用于 x 和 y 值)。
Next, I'm going to split A into its x and y values (x a and y a ).接下来,我要将 A 拆分为其 x 和 y 值(x a和 y a )。 The edges of the square (our binding box) intersect the circle at the horizontal and vertical (directly left, right, above or below the centre).
正方形(我们的装订盒)的边缘在水平和垂直方向(直接向左、向右、在中心上方或下方)与圆相交。 Taking the horizontal first, we therefore know that x a is x c minus the radius.
首先取水平,因此我们知道 x a是 x c减去半径。 In the same way, we can say that y a is y c minus the radius (using tkinter canvas coords - which work from the top-left to bottom-right rather than bottom-left to top-right).
以同样的方式,我们可以说 y a是 y c减去半径(使用 tkinter 画布坐标 - 从左上角到右下角而不是从左下角到右上角)。
Again, in a similar way, we can say that x b and y b (the x and y values of B) are x c and y c plus the radius.同样,以类似的方式,我们可以说 x b和 y b (B 的 x 和 y 值)是 x c和 y c加上半径。 Combining our coordinate values back together, we get:
将我们的坐标值重新组合在一起,我们得到:
A = (xc - r, yc - r)
B = (xc + r, yc + r)
These are the coordinates of the binding box so we can go and pass these straight to tkinter:这些是绑定框的坐标,因此我们可以直接将它们传递给 tkinter:
#/usr/bin/python3
CENTRE = (100, 100)
RADIUS1 = 20
RADIUS2 = 30
CENTRE_X, CENTRE_Y = CENTRE
import tkinter as tk
root = tk.Tk()
c = tk.Canvas(root, width=200, height=200)
c.pack()
c.create_oval(CENTRE_X - RADIUS1, CENTRE_Y - RADIUS1,
CENTRE_X + RADIUS1, CENTRE_Y + RADIUS1)
c.create_oval(CENTRE_X - RADIUS2, CENTRE_Y - RADIUS2,
CENTRE_X + RADIUS2, CENTRE_Y + RADIUS2)
root.mainloop()
The result is two concentric circles in the middle of our canvas.结果是我们画布中间的两个同心圆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.