如何使用tkinter从文件中读取的坐标创建多边形



我想显示哥伦比亚特区的图像,我有240个坐标来创建带有TKINTER的多边形。坐标被一个空间隔开,我想将它们"分开"并将它们附加到X和Y上。到目前为止,下面附加的程序还没有运行任何内容,只是说"操作完成"。预期的结果是在640x480的窗口中显示DC。

from Tkinter import Tk,Canvas
from PIL import Image,ImageTk
root = Tk()
canvas = Canvas(root, width=640, height=480, bg="white")
f = open("lab312.txt")
points = []
for n in range (1, 240):
   z = f.readline()
   coords= z.split(" ")
   x=float(coords[0])
   y=float(coords[1])
   points.append((12820*x+300,324*y+2198))
root.mainloop()

我收到的结果是一个不是480x640的窗口,它没有任何内容。

这是一个最小的工作示例,使用字符串列表作为输入'文件'。您的主要问题将从数据文件中使用的坐标转换为canvas绘图坐标在其各自的范围内,y都增加了。

import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=640, height=480, bg="white")
canvas.pack()
f = [
'50 50',
'100 10',
'200 200',
'100 300',
'75 200',
]
points = []
for line in f:
    x, y = map(int, line.split())
    points.extend((x, y))
canvas.create_polygon(*points, fill='red')
root.update()

最新更新