我正在创建一个程序,用Zelle的程序在窗口中打开世界地图图形.py. 它有一个功能是在地图上绘制点,另一个功能是在这些点出现在屏幕上1秒后取消绘制(绘制后存储在列表中)。我希望这些函数同时工作,但是当在线程中调用addDots()
函数时,它不会在窗口中绘制点,它只是暂停。下面是我运行的模块:
import thread
import threading
import time
import random
import sys
sys.path.append('..')
from Display import map
import tester
import datetime
dots = list(())
def deleteDots():
while 1==1:
tF = datetime.datetime.now()
a = 0
for i in range(len(dots)):
tD = tF - dots[i-a][2]
tD = int(str(tD)[5:7])
if tD >= 1:
map.deletePoint(dots[i-a][0],dots[i-a][1])
dots.pop(i-a)
a = a+1
def addDots():
oldResponseCount = tester.getResponseCount()
oldResponseCount = int(str(oldResponseCount))
while 1==1:
print(oldResponseCount)
newResponseCount = tester.getResponseCount()
newResponseCount = int(str(newResponseCount))
print(newResponseCount)
if(newResponseCount != oldResponseCount):
difference = newResponseCount - oldResponseCount
for i in range(difference):
lat = random.randint(-90,90)
long = random.randint(-180,180)
map.drawPoint(lat,long)
tI = datetime.datetime.now()
dots.append([lat,long,tI])
oldResponseCount = newResponseCount
if __name__ == '__main__':
threading.Thread(target=addDots).start()
threading.Thread(target=deleteDots).start()
下面是地图模块,它在图形窗口上绘制地图,包含绘制和删除点的功能:
^{pr2}$我该怎么做?在
有几个问题需要解决。首先,任何图形.py调用tkinter的命令(即导致绘制/取消绘制的命令)必须由主(主)线程发出。所以我们需要次线程来将绘图请求传递给主线程。在
其次,您的二级线程都在修改}列表,您需要同步(锁定)对这些列表的访问,以便一次只有一个线程可以修改或迭代它们。在
Circles
和{下面是我对您的代码的重新编写示例。我取消了map和tester例程,因为我只是用一个线程在窗口上放置点,然后在另一个线程的第二个旧值之后删除它们:
相关问题 更多 >
编程相关推荐