Python 如何正确使用 tkinter create_line() 坐标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40390746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:29:48  来源:igfitidea点击:

How to correctly use tkinter create_line() coordinates

pythontkintercoordinateslinelines

提问by Tkinter Sucks

This tutorial is using the size of the canvas as coordinates for the lines: http://effbot.org/tkinterbook/canvas.htm

本教程使用画布的大小作为线条的坐标:http: //effbot.org/tkinterbook/canvas.htm

However, if we edit the code to give the canvas no padding, we can see that this is not working correctly. If you look closely the second create_line() is not lining up with the corners correctly:

但是,如果我们编辑代码使画布没有填充,我们可以看到这不能正常工作。如果仔细观察,第二个 create_line() 没有正确地与角对齐:

from tkinter import *

master = Tk()

w = Canvas(master, width=200, height=100,bd=0,highlightthickness=0)
w.configure(bg="black")
w.pack()

w.create_line(0, 0, 200, 100, fill="red")
w.create_line(0, 100, 200, 0, fill="red")
master.mainloop()

Another example with a 3x3 canvas:

另一个使用 3x3 画布的示例:

from tkinter import *

root = Tk()
root.configure(bg="blue")
canvas = Canvas(root, width=3, height=3, borderwidth=0, highlightthickness=0, bg="black")
canvas.pack()

canvas.create_line(0,0,3,3, fill="red")
canvas.create_line(0,3,3,0, fill="red")

root.mainloop()

This problem seems to only effect lines going from bottom-left to top-right, or top-right to bottom-left.

这个问题似乎只影响从左下角到右上角,或从右上角到左下角的线。

If we change the coordinates of the second create_line() to -1 and 3 it now works correctly:

如果我们将第二个 create_line() 的坐标更改为 -1 和 3,它现在可以正常工作:

from tkinter import *

root = Tk()
root.configure(bg="blue")
canvas = Canvas(root, width=3, height=3, borderwidth=0, highlightthickness=0, bg="black")
canvas.pack()

canvas.create_line(0,0,3,3, fill="red")
canvas.create_line(-1,3,3,-1, fill="red")

root.mainloop()

My questions are: Why does this only effect the second create_line()? Why does the coordinate 0 become -1, if 3 does not become 2? Is this the way it's supposed to work, or does tkinter just have an inherent problem with drawing positive slopes correctly? It seems to me that the latter is the case. If I want to make a program that draws many lines based on a given set of coordinates, I would seemingly have to calculate if every given segment is a positive or negative slope before creating it.

我的问题是:为什么这只会影响第二个 create_line()?为什么坐标0变成-1,如果3没有变成2?这是它应该工作的方式,还是 tkinter 在正确绘制正斜率方面存在固有问题?在我看来,后者是这种情况。如果我想制作一个基于给定坐标集绘制多条线的程序,我似乎必须在创建之前计算每个给定的线段是正斜率还是负斜率。

I have had to put the program I'm making on a complete hold for several days because of this. Can somebody please provide any insight to this issue? Is there something I am missing or not understanding?

由于这个原因,我不得不将我正在制作的程序完全搁置几天。有人可以提供有关此问题的任何见解吗?有什么我遗漏或不理解的吗?

回答by furas

You make mistake in second line

你在第二行出错

  • Python counts from 0so left bottom corner is (0,2), not (0,3). So you have to start second line in point (0,2)

  • First line has points (0,0), (1,1), (2,2)and (3,3)which is outside of canvas. Similar second line should have (0,2), (1,1), (2,0)and (3,-1)which is outside of canvas. But you can't skip (3,3)and (3,-1)because create_linedoesn't draw last point - last point doesn't belong to line (similar like xdoesn't belong to range(x)). If you skip (3,3)and (3,-1)then create_linedoesn't draw (2,2)and (2,0)

  • Python 从0左下角开始计数(0,2),而不是(0,3)。所以你必须从第二行开始(0,2)

  • 第一行有点(0,0), (1,1), (2,2)并且(3,3)在画布之外。类似的第二行应该有(0,2), (1,1), (2,0)并且(3,-1)在画布之外。但是你不能跳过(3,3)(3,-1)因为create_line不绘制最后一点 - 最后一点不属于线(类似于x不属于range(x))。如果跳过(3,3)(3,-1)然后create_line不画(2,2)(2,0)

Correct lines

正确的线条

canvas.create_line(0,0,3,3, fill="red")
canvas.create_line(0,2,3,-1, fill="red")

in other words

换句话说

  • first line (a,b,a+3,b+3)gives (0,0,0+3,0+3)= (0,0,3,3)
  • second line (a,b,a-3,b-3)gives (0,2,0-3,2-3)= (0,2,3,-1)
  • 第一行(a,b,a+3,b+3)给出(0,0,0+3,0+3)=(0,0,3,3)
  • 第二行(a,b,a-3,b-3)给出(0,2,0-3,2-3)=(0,2,3,-1)