我用python做了一个卫星位置探测项目。但此代码引发了以下错误:
Traceback (most recent call last):
File "C:\Users\soumy\OneDrive\Desktop\PySatelliteFinder\main.py", line 50, in <module>
calculate_and_display_position(name.upper()) # Convert to uppercase for TLE lookup
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\soumy\OneDrive\Desktop\PySatelliteFinder\main.py", line 15, in calculate_and_display_position
satellite = ts[name]
~~^^^^^^
TypeError: list indices must be integers or slices, not str
我的代码是:
import turtle
from datetime import datetime
from skyfield.api import Topos, load
def calculate_and_display_position(name):
screen.clear()
ts = load.tle_file('stations.txt')
try:
satellite = ts[name]
except KeyError:
screen.write(f"Satellite '{name}' not found.", align="center", font=("Arial", 16, "normal"))
return
current_time = datetime.utcnow()
topos = satellite.at(ts.utc(current_time.year, current_time.month, current_time.day,
current_time.hour, current_time.minute, current_time.second))
alt, az, _ = topos.observe(observer_location).apparent().altaz()
screen.write(f"Satellite: {name}\n"
f"Altitude: {alt.degrees:.2f} degrees\n"
f"Azimuth: {az.degrees:.2f} degrees\n"
f"Local Time: {current_time}", align="left", font=("Arial", 16, "normal"))
screen = turtle.Screen()
screen.bgpic("world_map.gif")
observer_location = Topos(latitude_degrees=23.247921, longitude_degrees=87.871365)
satellite_turtle = turtle.Turtle()
satellite_turtle.penup()
satellite_turtle.goto(0, 200)
satellite_turtle.hideturtle()
name = screen.textinput("Enter Satellite Name", "Enter the satellite name:")
if name:
calculate_and_display_position(name.upper())
screen.onclick(lambda x, y: calculate_and_display_position(screen.textinput("Enter Satellite Name", "Enter the satellite name:").upper()))
screen.exitonclick()
现在,如果我单击“确定”按钮,那么它就会发生:
The Moment When The Code Is Run, It Throws A Dialog Asking For The Name
我试着把
ts = load.tle_file('stations.txt')
的外部
calculate_and_display_position(name)
和里面
calculate_and_display_position(名称)
,我写
global ts
但仍然显示出相同的东西。
我正在分析一个输出,当输入最新的卫星名称时,模块将获取坐标并在地图上做一个点,并用红色显示名称的位置。