gpt4 book ai didi

python - 在 python GUI 中使用坐标列表创建一条线

转载 作者:行者123 更新时间:2023-12-01 05:44:41 25 4
gpt4 key购买 nike

我一直在尝试使用 create_line 和 (x,y) 点列表创建一个图形。

import Tkinter
Screen = [a list of screen coordinates]
World = []
for x,y in screen:
World.append(somefunctiontochange(x,y))
if len(World) >= 2:
Canvas.create_line(World)

尽管该行没有显示在我的 Canvas 中,并且没有给出错误。有什么帮助吗?

最佳答案

花了我一段时间,但这就是你如何以你想要的方式在 Canvas 上绘制:

import Tkinter as tk

root = tk.Tk()
root.geometry("500x500")
root.title("Drawing lines to a canvas")

cv = tk.Canvas(root,height="500",width="500",bg="white")
cv.pack()

def linemaker(screen_points):
""" Function to take list of points and make them into lines
"""
is_first = True
# Set up some variables to hold x,y coods
x0 = y0 = 0
# Grab each pair of points from the input list
for (x,y) in screen_points:
# If its the first point in a set, set x0,y0 to the values
if is_first:
x0 = x
y0 = y
is_first = False
else:
# If its not the fist point yeild previous pair and current pair
yield x0,y0,x,y
# Set current x,y to start coords of next line
x0,y0 = x,y

list_of_screen_coods = [(50,250),(150,100),(250,250),(350,100)]

for (x0,y0,x1,y1) in linemaker(list_of_screen_coods):
cv.create_line(x0,y0,x1,y1, width=1,fill="red")

root.mainloop()

您需要为 create_line 提供线条起点和终点的 x,y 位置,在上面的示例代码中(有效)我​​正在绘制连接点 (50,250),(150,100),(250,250 ),(350,100) 呈锯齿形

还值得指出的是, Canvas 上的 x,y 坐标从左上角而不是左下角开始,不要把它想象成一个 x,y = 0,0 位于左下角的图表。 Canvas 以及更多如何打印到页面,从左上角开始向右移动 x 轴,并随着页面向下移动而 y 轴递增。

我用过: http://www.tutorialspoint.com/python/tk_canvas.htm作为引用。

关于python - 在 python GUI 中使用坐标列表创建一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16494896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com