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

如何将Tkinter中获取的鼠标坐标转换为经纬度?

Convert Tkinter Map Click Coordinates to Latitude/Longitude

Hey there! Let's get your GeoGuessr-style game's coordinate conversion sorted out—sounds like a fun project!

First, a key point you need to define upfront: the actual geographic bounds of your map. For example, if it's a full world map, your longitude range would typically be -180° to 180° and latitude 90° N to 90° S (note: map Y-coordinates usually run top-to-bottom, which is opposite to latitude's north-to-south). If it's a regional map, you'll just swap in that region's min/max lat/lon values.

Step 1: Core Conversion Logic

The conversion uses linear scaling since your window and map are the same size (no resizing to worry about!). Here's the formula breakdown:

Longitude Calculation

  • Map width: 1440px
  • Let min_lon = your map's westernmost longitude (e.g., -180)
  • Let max_lon = your map's easternmost longitude (e.g., 180)
  • longitude = min_lon + (x / map_width) * (max_lon - min_lon)

Latitude Calculation

  • Map height: 715px
  • Let max_lat = your map's northernmost latitude (e.g., 90)
  • Let min_lat = your map's southernmost latitude (e.g., -90)
  • latitude = max_lat - (y / map_height) * (max_lat - min_lat)
    (We subtract here because Y=0 is the top of your map, which corresponds to the highest latitude.)

Step 2: Tkinter Implementation Code

Here's a complete example that ties this into your Tkinter setup, including capturing mouse clicks and converting the coordinates:

import tkinter as tk

# Define your map's geographic bounds (adjust these for your specific map!)
MIN_LON = -180.0
MAX_LON = 180.0
MAX_LAT = 90.0
MIN_LAT = -90.0

MAP_WIDTH = 1440
MAP_HEIGHT = 715

def click_to_latlon(x, y):
    # Convert x to longitude
    lon = MIN_LON + (x / MAP_WIDTH) * (MAX_LON - MIN_LON)
    # Convert y to latitude
    lat = MAX_LAT - (y / MAP_HEIGHT) * (MAX_LAT - MIN_LAT)
    return (round(lat, 4), round(lon, 4))  # Round for readability

def on_mouse_click(event):
    x, y = event.x, event.y
    lat, lon = click_to_latlon(x, y)
    print(f"Clicked at ({x}, {y}) → Latitude: {lat}, Longitude: {lon}")
    # You can update a label or use these values in your game logic here

# Set up the Tkinter window
root = tk.Tk()
root.geometry(f"{MAP_WIDTH}x{MAP_HEIGHT}")
root.title("GeoGuessr Clone")

# Load your map image (replace 'your_map.png' with your actual image path)
map_image = tk.PhotoImage(file="your_map.png")
map_label = tk.Label(root, image=map_image)
map_label.pack(fill=tk.BOTH, expand=True)

# Bind mouse click event to the label
map_label.bind("<Button-1>", on_mouse_click)

root.mainloop()

Step 3: Adjustments for Your Map

  • If your map isn't a full world map, just update MIN_LON, MAX_LON, MAX_LAT, MIN_LAT to match your map's actual boundaries. For example, if you're focusing on Europe, you might use MIN_LON=-10, MAX_LON=30, MAX_LAT=70, MIN_LAT=35.
  • If you ever resize your map/window later, you'll need to get the current size of the map widget instead of using fixed MAP_WIDTH/MAP_HEIGHT (you can use map_label.winfo_width() and map_label.winfo_height() for that).

Quick Note on Map Projections

If your map uses a complex projection (like Mercator), this linear conversion will have slight inaccuracies. But for a casual GeoGuessr-style game, linear scaling is usually more than enough to get reliable coordinates!

内容的提问来源于stack exchange,提问作者Mattéo Christophe

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 18:12:38