0
初次使用者。我是喬治梅森大學的一名研究生,我受到了很大的幫助,並且受到了社區的印象,所以我想我會提出一個問題。我想繪製使用tkinter從文本文件中指定的點(點在代碼的頂部指定,但我把它們放在一個文本文件中)。我的任務需要我來投射點,但我想更進一步,讓他們直接從我的變量中讀取,所以如果.txt改變,他們也會改變。我的問題是,我不知道如何解析變量,以便tkinter可以看到它們。Python:如何在讀取.txt時使用tkinter繪製座標
Here is the code:
"""
Read the following data (I put them into .txt first):
Polyline;
1: 1603714.835939442,142625.48838266544; 1603749.4678153452,142620.21243656706; 1603780.3769339535,142607.37201781105; 1603801.475846678,142582.27024446055; 1603830.4767344964,142536.14692804776;
2: 1602514.2066492266,142330.66992144473; 1602521.4127475217,142414.92978276964; 1602520.1146955898,142433.93817959353; 1602501.3840010355,142439.12358761206; 1602371.6780588734,142417.84858870413; 1602351.6610373354,142408.02716448065; 1602334.5180692307,142388.58748627454; 1602331.6999511716,142376.66073128115; 1602334.8067251327,142348.965322732; 1602338.308919772,142323.6111663878; 1602349.0226452332,142314.50124930218; 1602363.9090971674,142310.79584660195; 1602514.2066492266,142330.66992144473;
The following code define a function 'readPolylineFile' to read out data one line by one line
The readPolylineFile function will return two polylines
In addtion, try....except...finally are used to catch the exceptions
"""
import math
class Points:
def __init__(self, x=0.0, y=0.0):
self.x,self.y = x, y
class Polyline:
def __init__(self, points =[]):
self.points = points
def getLength(self):
i = 0
length = 0.0
while i < len(self.points)-1:
length += math.sqrt((self.points[i+1].x -self.points[i].x)**2 + (self.points[i+1].y -self.points[i].y)**2)
i += 1
return length
## function to read out data one line by one line
## return polylines list
def readPolylineFile(fileName):
## Declare variables that will be used outside of try blocks
polylines = [] ## empty polyline list to keep all polylines
f = None # empty file object
try:
f = open(fileName, 'r') ## Open the file and assign the return object to f
firstPolyLineNum = 0
index = 0
for line in f:
index += 1
if index == 1: ## Pass the first line
continue
coords = line.split(':')[1]
eachcoords = coords.split(';')
coordsLen = len(eachcoords)
points = [] ## Declare a points list to keep the points for each polyline
for i in range(coordsLen-1):
singlecoords = eachcoords[i]
xCoord = singlecoords.split(',')[0]
yCoord = singlecoords.split(',')[1]
print singlecoords
try:
point = Points(float(xCoord),float(yCoord))
points.append(point)
except ValueError:
print 'Can not convert non-number to float'
except TypeError:
print 'Object type can not conver to float'
## Create a polyline object based on the points
polyline = Polyline(points)
polylines.append(polyline)
except IOError: ##File does not exist
print 'IO Error while opening or reading the file'
finally:
if f: ## If file object exist, close the file
print 'Input .txt has been read and is now closed......'
f.close()
return polylines ## Return the polylines
polylines = readPolylineFile('F://GMU Fall 2013/GGS 650/HW6/input.txt')
try:
## Get the first polyline
polyLine1 = polylines[0]
lengthForFirstPoly = polyLine1.getLength()
print "length for first polyline (first 5 coordinates) -> ", lengthForFirstPoly
## Gets the points for second polyline
polyLine2 = polylines[1]
lengthForSecondPoly = polyLine2.getLength()
print "length for Second polyline (last 13 coordinates) -> ", lengthForSecondPoly
except:
print "error in processing polyline objects created"
from Tkinter import *
master = Tk()
master2 = Tk()
w = Canvas(master, width=800, height=600)
w.pack()
w.create_line(0, 0, 200, 100)
w = Canvas(master2, width=800, height=600)
w.pack()
w.create_line(0, 0, 200, 297)
mainloop()
你甩了一個整體噸的代碼,並沒有告訴我們哪部分不能正常工作,甚至沒有告訴我們哪部分工作不正常。請閱讀[SSCCE](http://sscce.org)瞭解如何讓您的問題更容易回答。 – abarnert
什麼是實際問題/錯誤? – Ivo
對不起,長碼。我沒有意識到這裏存在慣例,不過,這很有意義,因爲較短的代碼更容易排除故障。問題不是一個錯誤,而是我不知道插入tkinter(底部)來繪製我的座標(頂部),我不想使用靜態值,即使這足以獲得好的因爲我爲自己的畢業學校付錢,所以我真的想學點東西。通過能夠讀取.txt中的座標並繪製它們,我將擁有一項有用的技能。 – user2850932