我是python的乞丐,我希望有人能帮我,我身陷困境。我正在写一个简单的代码,它通过ITCIP与里程表连接,接收数据,处理数据并存储在字典中,当到达某个定义的距离时,while循环中断并返回一个包含数据的字典。这些功能单独起作用是完美的。你知道吗
当我想用Tkinter从GUI执行函数时,问题来了,我把函数分配给一个按钮,当按下按钮时,函数启动,但在最后一个循环中被阻止,如果我关闭GUI,函数在控制台中显示数据,但不返回单词,任何错误都会出现。我尝试使用“lambda:”,如果没有它,则使用“execfile()”执行一个外部脚本,在一个外部文件中调用函数,并且函数永远不会结束。只有当ist与Crt+c断开时,这里有一个简化的代码“奥多姆·皮“:”
from Tkinter import *
import socket
from datetime import datetime, timedelta
v0=Tk()
v0.config(bg="gray")
v0.geometry("400x300")
v0.title("GUI")
def func():
ITCIPadress='172.17.18.21'
port=6000
i=0 #initialize the dictionary
rueda = {i:{
'position':i,
'distance':i}}
distancetomeasure=float(raw_input("distance to measure(in m): "))
dist=float(0)
distincr=float(0)
rev=0.5 ##odometer wheel circunference in meter
stepsrev=8192.0#Number of steps/revolution in the odometer
s = socket.socket()
s.connect((ITCIPadress, port))
while dist<=distancetomeasure: #It works in positive and negative direction
recibido = s.recv(45)#number of bytes recived
print "Recibido:", recibido
Postime_tmp=map(int, re.findall('\d+', recibido))#split the data
position_tmp=int(Postime_tmp[0])
rueda.update({i:{'position':Postime_tmp[0],
'distance':dist}})
if i>0: #because when i=0 there is no increment
incr_tmp=(rueda[i]['position']-rueda[i-1]['position'])
distincr=(incr_tmp/stepsrev)*rev
print distincr
dist=dist+distincr
print 'the distance till yet is:', dist
rueda.update({i:{'position':Postime_tmp[0],
'distance':dist}})
i=i+1
print "the distance from start point is:", dist
s.close()
return rueda
b1=Button(v0,text='Start the odometer',command= lambda: func())
b1.pack(fill=BOTH, expand=1) #command= execfile(C:\odometer.py)[calling another file with the function]
v0.mainloop() #command= lambda: odometer.measuredistance()[calling another file with the function]
#if __name__ == '__main__':
# rueda = func()
如果我对GUI的某个部分进行注释,并取消对最后两行的阻塞,那么它可以很好地工作,但在GUI中就不行了。我无法理解为什么会发生这种行为。你知道吗
这是GUI,测量1米,它堆叠在最后:
0.037109375
the distance till yet is: 0.896179199219
Recibido: POSITION=25403046 TIMESTAMP=104308321
0.0422973632812
the distance till yet is: 0.9384765625
Recibido: POSITION=25403756 TIMESTAMP=104502033
0.0433349609375
the distance till yet is: 0.981811523438
Recibido:
这只执行了一个功能,也测量了1米:
0.037109375
the distance till yet is: 0.896179199219
Recibido: POSITION=25403046 TIMESTAMP=104308321
0.0422973632812
the distance till yet is: 0.9384765625
Recibido: POSITION=25403756 TIMESTAMP=104502033
0.0433349609375
the distance till yet is: 0.981811523438
Recibido: POSITION=25404477 TIMESTAMP=104705956
0.0440063476562
the distance till yet is: 1.02581787109
the distance from start point is: 1.02581787109
最后一个还返回包含所有感兴趣值的字典。任何帮助都将不胜感激!谢谢
首先,您不应该在由
GUI
调用的函数中使用raw_input
。相反,试着用“distancetomeasure”变量的输入字段构建GUI
。你知道吗之后,与其返回结果,不如构建另一个小部件
GUI
来显示这些结果?你知道吗我上次使用
Tkinter
已经有很长时间了,但是这是一个不错的GUI
,如果你有时间,你应该知道如何通过访问http://effbot.org/tkinterbook/tkinter-index.htm来构建一个不错的应用程序。你知道吗但如果你赶时间,我相信这里的其他人会很乐意帮助你:)
你没有看到任何返回的原因是它实际上返回到你用来调用函数的按钮,正如@Yann所说,你应该尝试构建你的GUI来完成你的整个程序,否则让GUI和命令行都对用户可见是没有意义的
相关问题 更多 >
编程相关推荐