我目前正在尝试编写一个非常基本的程序,该程序将从本地运行的另一个程序中获取OSC数据(以X-Y坐标的形式)。我希望将这些数据放入同一脚本(Python 3.6)中的Turtle中,然后可以在窗口上放置点以方便可视化。
不幸的是,每次尝试运行服务器时,都会出现以下错误:
文件“”,第1行,在坐标文件“ C:\ Users \ Rebekah中 Castillo \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ tkinter__init __。py“, 2466行,在坐标 self.tk.call(((self._w,'coords')+ args))] RuntimeError:主线程不在主循环中
下面是我的代码。从一些阅读中,我相信这与Turtle打开一个新线程有关,并且有某种方法可以通过Queue模块对其进行修复。不过,在那之后,我很困惑,也非常感谢任何建议。
import argparse
import random
import time
import math
import turtle
from pythonosc import dispatcher
from pythonosc import osc_server
from threading import Thread
subject = turtle.Turtle()
subject.pendown()
subject.hideturtle()
def location_handler(unused_addr,args,locationx,locationy):
print("{0} ~ {1}".format(locationx,locationy))
subject.goto(locationx,locationy)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--ip",default="127.0.0.1",help="The ip of the OSC server")
parser.add_argument("--port",type=int,default=5005, help="The port the OSC server is listening on")
args = parser.parse_args()
dispatcher = dispatcher.Dispatcher()
#This will print all parameters to stdout, a file in which kernel writes its output; all exceptions go to stderr
dispatcher.map("/location",location_handler,"Location")
server = osc_server.ThreadingOSCUDPServer((args.ip,args.port),dispatcher)
print("Serving on {}".format(server.server_address))
server.serve_forever()
谢谢!
-OH