为什么 ts[name] 东西在 python 中不断给出错误

问题描述 投票:0回答:1

我正在用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

# Function to calculate and display satellite position
def calculate_and_display_position(name):
    # Clear previous drawing
    screen.clear()
    
    # Load TLE data
    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

    # Get current UTC time
    current_time = datetime.utcnow()
    
    # Calculate satellite position
    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()

    # Display satellite name, position, and local time
    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"))

# Create a Turtle screen
screen = turtle.Screen()
screen.bgpic("world_map.gif")  # You'll need to provide a world map image

# Create an observer location
observer_location = Topos(latitude_degrees=23.247921, longitude_degrees=87.871365)

# Initialize the Turtle
satellite_turtle = turtle.Turtle()
satellite_turtle.penup()
satellite_turtle.goto(0, 200)
satellite_turtle.hideturtle()

# Input box for satellite name
name = screen.textinput("Enter Satellite Name", "Enter the satellite name:")
if name:
    calculate_and_display_position(name.upper())  # Convert to uppercase for TLE lookup

# Listen for clicks to change satellite
screen.onclick(lambda x, y: calculate_and_display_position(screen.textinput("Enter Satellite Name", "Enter the satellite name:").upper()))

# Close the window when clicked
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(name)
分开,我写了
global ts
但仍然显示相同的内容。

我期待一个输出,当输入最新的卫星名称时,模块将获取坐标并在地图上做一个点,并以红色显示该名称的位置。

windows datetime turtle-graphics python-3.8 skyfield
1个回答
0
投票

将您的代码与 Skyfield 的 Earth Satellites 页面上的示例进行比较,看起来您缺少将卫星列表转换为按卫星名称索引的字典的行:

by_name = {sat.name: sat for sat in satellites}

适应你自己的代码,也许:

ts_list = load.tle_file('stations.txt')
ts = {sat.name: sat for sat in ts_list}
© www.soinside.com 2019 - 2024. All rights reserved.