我想做的事:
- 在线程中运行 TCPServer
- 获取数据“从套接字接收”
- 更新绘图并显示数据
我成功地做了什么:
- wxPython 的框架有一个按钮,
- 单击按钮启动套接字
- 从套接字接收数据
问题:
当我尝试从 TCPServer 线程调用绘图函数时,我收到此错误
"PyAssertionError: C++ assertion "wxThread::IsMain()" failed at ..\..\src\common\timercmn.cpp(66) in wxTimerBase::Start(): timer can only be started from the main thread "
编码:
Main,我开始线程的地方
def start_socket(self): HOST = '' PORT_A = 6340 self.server_A = ThreadedTCPServer((HOST, PORT_A), ThreadedTCPRequestHandler) self.server_temp = self.server_A self.server_A_thread = threading.Thread(target=self.server_A.serve_forever) self.server_A_thread.setDaemon(True) self.server_A_thread.start()
我尝试绘制的线程:
class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler): def handle(self): self.Main = Main(None, -1, '') self.data = self.request.recv(1024).strip() print "%s wrote: " % self.client_address[0] print self.data plt.ion() self.fig = plt.figure(1) print self.image_scale ax = self.fig.add_subplot(111) self.image_scale = ([1,2], [ 4,10 ] ) cax = plt.imshow(self.image_scale, cmap='Greys', interpolation='nearest' ) draw()
通过调试,错误来自这一行:
self.fig = plt.figure(1)
Error: "timer can only be started from the main thread"
所以,不确定,但我想我需要在 wx.Frame 的主函数中进行绘图,是否有任何解决方案可以保持正常程序运行,并允许在另一个线程中实时绘图?特别是我打算制作多个 TCPServer 线程。
提前感谢您,我们将非常感谢您的帮助。