最近在学习python,楼主练习写个程序遇到个问题,好像是全局变量的问题吧,我用return时能正常,但写GUI时不会用return了,结果读不进数据,有python高手请指导下。
普通程序:(能正常执行)
#读取路权矩阵
def readdata(D0,S0,D1,S1,list):
fpr=open("d.txt")
k=0
for line in fpr:
k+=1
D0[k]={}
S0[k]={}
S1[k]={}
D1[k]={}
list=line.split()
for j in range(len(list)):
D0[k][j+1]=int(list[j])
S0[k][j+1]=j+1
S1[k][j+1]=0
D1[k][j+1]=0
fpr.close()
return (D0,S0,D1,S1,list)
#最短路算法
def solve(D0,S0,D1,S1,list):
for k in range(1,len(list)+1):
for k1 in range(1,len(list)+1):
for j1 in range(1,len(list)+1):
if D0[k1][j1]<=D0[k1][k]+D0[k][j1]:
S1[k1][j1]=S0[k1][j1]
else:
S1[k1][j1]=S0[k1][k]
D1[k1][j1]=min(D0[k1][j1],D0[k1][k]+D0[k][j1])
if k<len(list):
D0=D1
S0=S1
return (D0,S0,D1,S1,list)
#将路径和路权的结果写入文件中
def write_lujingluquan(D1,S1,list):
fpw1=open("s1_res.txt","w+")
fpw2=open("d1_res.txt","w+")
for k in range(1,len(list)+1):
for k1 in range(1,len(list)+1):
fpw1.write("%d\t"%S1[k][k1])
fpw2.write("%d\t"%D1[k][k1])
if k1==len(list):
fpw1.write("\n")
fpw2.write("\n")
fpw1.close()
fpw2.close()
#将最短路结果写入文件中
def write_shortpath(D1,S1,list):
fpw=open("solve.txt","w+")
for k in range(1,len(list)+1):
for k1 in range(1,len(list)+1):
if k!=k1:
fpw.write("%d->%d\t%d\t%d"%(k,k1,D1[k][k1],k))
temp=k
while S1[temp][k1]!=k1:
fpw.write("->%d"% S1[temp][k1])
temp=S1[temp][k1]
fpw.write("->%d\n"%S1[temp][k1])
fpw.close()
#查询
def research(k,k1):
if k!=k1:
print(" %d->%d\t %d\t %d"%(k,k1,D1[k][k1],k)),
temp=k
while S1[temp][k1]!=k1:
print("->%d"% S1[temp][k1]),
temp=S1[temp][k1]
print("->%d"%S1[temp][k1])
else:
print("%d->%d\t%d\t%d->%d"%(k,k1,0,k,k1))
#函数入口
#初始化字典
D0={}
S0={}
S1={}
D1={}
list=[]
(D0,S0,D1,S1,list)=readdata(D0,S0,D1,S1,list)
(D0,S0,D1,S1,list)=solve(D0,S0,D1,S1,list)
write_lujingluquan(D1,S1,list)
write_shortpath(D1,S1,list)
a=input("查询两个节点的最短请输入1:")
if a==1:
start=input("请输入起点:")
end=input("请输入终点:")
print "查询结果如下:"
print "起终点\t最短路长\t路径"
research(start,end)
#print "succeed!"
楼主想要GUI编程来实现,程序如下:(调试了下,好像不能把"d.txt"数据读进来,或者读了,等执行完readdata()方法后又消失了)
#!/suer/bin/python
#-*-coding:UTF-8-*-
from Tkinter import *
#初始化字典
D0={}
S0={}
S1={}
D1={}
list=[]
app=Tk()
#读取路权矩阵
def readdata(D0,S0,D1,S1,list):
fpr=open("d.txt")
k=0
for line in fpr:
k+=1
D0[k]={}
S0[k]={}
S1[k]={}
D1[k]={}
list=line.split()
for j in range(len(list)):
D0[k][j+1]=int(list[j])
S0[k][j+1]=j+1
S1[k][j+1]=0
D1[k][j+1]=0
fpr.close()
#return (D0,S0,D1,S1,list)
#最短路算法
def solve(D0,S0,D1,S1,list):
for k in range(1,len(list)+1):
for k1 in range(1,len(list)+1):
for j1 in range(1,len(list)+1):
if D0[k1][j1]<=D0[k1][k]+D0[k][j1]:
S1[k1][j1]=S0[k1][j1]
else:
S1[k1][j1]=S0[k1][k]
D1[k1][j1]=min(D0[k1][j1],D0[k1][k]+D0[k][j1])
if k<len(list):
D0=D1
S0=S1
return (D0,S0,D1,S1,list)
#将路径和路权的结果写入文件中
def write_lujingluquan(D1,S1,list):
fpw1=open("s1_res.txt","w+")
fpw2=open("d1_res.txt","w+")
for k in range(1,len(list)+1):
for k1 in range(1,len(list)+1):
fpw1.write("%d\t"%S1[k][k1])
fpw2.write("%d\t"%D1[k][k1])
if k1==len(list):
fpw1.write("\n")
fpw2.write("\n")
fpw1.close()
fpw2.close()
#将最短路结果写入文件中
def write_shortpath(D1,S1,list):
fpw=open("solve.txt","w+")
for k in range(1,len(list)+1):
for k1 in range(1,len(list)+1):
if k!=k1:
fpw.write("%d->%d\t%d\t%d"%(k,k1,D1[k][k1],k))
temp=k
while S1[temp][k1]!=k1:
fpw.write("->%d"% S1[temp][k1])
temp=S1[temp][k1]
fpw.write("->%d\n"%S1[temp][k1])
fpw.close()
#查询
def research(k,k1):
if k!=k1:
print(" %d->%d\t %d\t %d"%(k,k1,D1[k][k1],k)),
temp=k
while S1[temp][k1]!=k1:
print("->%d"% S1[temp][k1]),
temp=S1[temp][k1]
print("->%d"%S1[temp][k1])
else:
print("%d->%d\t%d\t%d->%d"%(k,k1,0,k,k1))
b1=Button(app,text="读取数据",command=lambda:readdata(D0,S0,D1,S1,list)).pack()
b2=Button(app,text="算法",command=lambda:solve(D0,S0,D1,S1,list)).pack()
b3=Button(app,text="write_lujingluquan",command=lambda:write_lujingluquan(D1,S1,list)).pack()
b4=Button(app,text="write_shortpath",command=lambda:write_shortpath(D1,S1,list)).pack()
app.mainloop()