You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3D双温度计线程实现时报can't register atexit after shutdown错误求助

错误原因
  • 你使用的VPython库要求所有渲染相关操作(包括rate()帧率控制、修改3D对象属性)必须在主线程执行,子线程中调用这些接口会触发内部退出回调注册冲突,就是你遇到的can't register atexit after shutdown报错的根源。
  • 额外发现你的代码存在一个小疏漏:第二个温度计的刻度创建时,pos参数漏掉了z轴的pos2值,会导致两个温度计的刻度重叠在第一个的位置。
解决思路

完全不需要引入线程实现两倍速需求,直接在主线程的同一个循环内,控制两个汞柱的更新步长为2倍关系即可,既符合VPython的渲染规则,逻辑也更简单。

修正后可运行代码
import numpy as np
from vpython import *

pos2=-5
mcr=1.7
r=2
br=r*1.2

# 第一个温度计
tc=cylinder(pos=vector(0,0,0),size=vector(9,r,r),color=color.white,opacity=0.35)
tm=cylinder(pos=vector(0.2,0,0),size=vector(1,mcr,mcr),color=color.red)
tb=sphere(pos=vector(-(r/5),0,0),size=vector(br,br,br),color=color.white,opacity=0.35)
mb=sphere(pos=vector(-(r/5),0,0),size=vector(br-.2,br-.2,br-.2),color=color.red)
for j in range(1,9,1):
    Marker=box(pos=vector(j,-(r/2)+0.15,0),size=vector(0.1,.1,r/2-0.15),color=color.white,opacity=0.8)

# 第二个温度计
tc2=cylinder(pos=vector(0,0,pos2),size=vector(9,r,r),color=color.white,opacity=0.35)
tm2=cylinder(pos=vector(0.2,0,pos2),size=vector(1,mcr,mcr),color=color.red)
tb2=sphere(pos=vector(-(r/5),0,pos2),size=vector(br,br,br),color=color.white,opacity=0.35)
mb2=sphere(pos=vector(-(r/5),0,pos2),size=vector(br-.2,br-.2,br-.2),color=color.red)
for j in range(1,9,1):
    Markers=box(pos=vector(j,-(r/2)+0.15,pos2),size=vector(0.1,.1,r/2-0.15),color=color.white,opacity=0.8)

# 主循环控制两个汞柱运动
length1 = 1.0
length2 = 1.0
speed1 = 0.015
speed2 = speed1 * 2
direction1 = 1
direction2 = 1

while True:
    rate(250)
    # 更新第一个汞柱
    length1 += speed1 * direction1
    if length1 >= 8.5 or length1 <=1:
        direction1 *= -1
    tm.size = vector(length1, mcr, mcr)
    # 更新第二个汞柱
    length2 += speed2 * direction2
    if length2 >= 8.5 or length2 <=1:
        direction2 *= -1
    tm2.size = vector(length2, mcr, mcr)

内容的提问来源于stack exchange,提问作者RedLegend 27

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 01:30:01